How to correctly use auto_created attribute in django?

前端 未结 1 758
遥遥无期
遥遥无期 2021-02-19 18:08

I need to create my own intermediate model.

class class1(models.Model):
    pass

class class2(models.Model):
    field1 = models.ManyToManyField(class1, through=         


        
1条回答
  •  庸人自扰
    2021-02-19 18:32

    As far as I am aware, the auto_created attribute in the Meta class is undocumented, so you should avoid using it.

    As the AttributeError says, it is not possible to use add() for a many to many field that uses an intermediary model. The correct fix is to create an instance of the intermediate model, instead of using add().

    class3.objects.create(field_1=c1, field_2=c2, field_3=1).
    

    See the docs on extra fields in many to many relationships for more info.

    0 讨论(0)
提交回复
热议问题