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

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

问题描述

我有 2 个表用户和域之间的多对多关系.我在 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天全站免登陆