Django管理表单为多对多关系 [英] Django Admin Form for Many to many relationship

查看:153
本文介绍了Django管理表单为多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表之间的许多关系用户一个域。
我已经在Domains类中定义了这种关系。
所以在管理界面中,当我查看域名时,我看到用户。
但是当我查看用户时,我看不到域名。
我如何实现这个。

I have a many to many relationship between 2 tables Users an Domains. I have defined this relationship in the Domains class. So in the admin interface I see the Users when I am viewing Domains. But I do not see Domains when I am viewing Users. How can I achieve this.

推荐答案

我知道这是一个旧的线程,但这是第一个结果在google上出现,我认为需要更好的答案。

I know that this is an older thread, but this was the first result that came up on google and I thought a better answer was necessary.

通过此django错误报告我发现最好的方式让你的ManyToManyField显示在这两个模型上:

Via this django bug report I found the best way to have your ManyToManyField show up on both models:

class Test1(models.Model):
    tests2 = models.ManyToManyField('Test2', blank=True)

class Test2(models.Model):
    tests1 = models.ManyToManyField(Test1, through=Test1.tests2.through, blank=True)

我已经测试了自己,对结果非常满意。

I have tested it myself and was very pleased with the results.

这篇关于Django管理表单为多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆