我需要创建自己的中间模型。
class class1(models.Model)
class class2(models.Model):
field1 = models.ManyToManyField(class1, through="class3")
class class3(models.Model):
field1 = models.ForeignKey(class1)
field2 = models.ForeignKey(class2)
field3 = models.IntegerField()
class Meta:
auto_created = True
我使用" auto_created = True"因为在下面的代码中,我有错误:
AttributeError:不能在指定中间模型的ManyToManyField上使用add()。
for m2m_field in self._meta.many_to_many:
for m2m_link in getattr(self, m2m_field.get_attname()).all():
getattr(to_object, m2m_field.get_attname()).add(m2m_link)
现在它运行正常,但是当我尝试进行makemigration时,django想要删除我的class3(中间类),并删除"通过" class2中field1中的属性。
我做错了什么?任何解决方案?
全力以赴。