我需要通过同一个关系表使用ManyToManyField将多个字段从一个模型连接到另一个模型。
class First(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
foo = models.ManyToManyField(
'Second',
related_name='foo',
through='RelationModel',
through_fields=('first', 'second')
)
bar = models.ManyToManyField(
'Second',
related_name='bar',
through='RelationModel',
through_fields=('first', 'second')
)
baz = models.ManyToManyField(
'Second',
related_name='baz',
through='RelationModel',
through_fields=('first', 'second')
)
class RelationModel(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
status = models.IntegerField(default=0)
first = models.ForeignKey('First')
second = models.ForeignKey('Second')
class Second(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
这会导致以下错误:
myapp.First :( models.E003)该模型通过中间模型“myapp.RelationModel'”有两个多对多关系。
我该如何解决这个问题?为什么不允许这样做?