我不明白为什么我们必须使用 ManyToManyField 来声明多对多关联。为此,我将创建另一个带有两个外键的表,句号!这是文档https://docs.djangoproject.com/en/dev/topics/db/models/#intermediary-manytomany中的一个示例:
from django.db import models
class Person(models.Model):
name = models.CharField(max_length=128)
class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(Person, through='Membership')
class Membership(models.Model):
person = models.ForeignKey(Person)
group = models.ForeignKey(Group)
extraField = models.DateField()
我只想写:
from django.db import models
class Person(models.Model):
name = models.CharField(max_length=128)
class Group(models.Model):
name = models.CharField(max_length=128)
class Membership(models.Model):
person = models.ForeignKey(Person)
group = models.ForeignKey(Group)
extraField = models.DateField()
这是对的吗?有什么区别?