Django Admin Form for Many to many relationship

前端 未结 4 1284
无人共我
无人共我 2020-12-24 08:15

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

4条回答
  •  旧时难觅i
    2020-12-24 08:38

    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.

    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.

提交回复
热议问题