为什么要用“通过” Django 模型中 ManyToManyField 的参数?
查看 Django 文档并尝试找出“through”参数的用法。这是
示例:
class Person(models.Model):
name = models.CharField(max_length=128)
def __unicode__(self):
return self.name
class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(Person, through='Membership')
def __unicode__(self):
return self.name
class Membership(models.Model):
person = models.ForeignKey(Person)
group = models.ForeignKey(Group)
date_joined = models.DateField()
invite_reason = models.CharField(max_length=64)
为什么需要 Group 的“members”属性?难道 Membership 的“组”ForeignKey 不足以跟踪关系并访问该信息吗?
Looking through the Django docs and trying to figure out the use of the "through" argument. Here is a link to the doc.
The example:
class Person(models.Model):
name = models.CharField(max_length=128)
def __unicode__(self):
return self.name
class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(Person, through='Membership')
def __unicode__(self):
return self.name
class Membership(models.Model):
person = models.ForeignKey(Person)
group = models.ForeignKey(Group)
date_joined = models.DateField()
invite_reason = models.CharField(max_length=64)
Why is the "members" attribute of Group even needed? Isn't the 'group' ForeignKey of Membership enough to follow the relation and access that information?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为你对这个问题的思考有点过于字面意思了。假设您没有使用
through
:Django 在幕后本质上为您创建了以下模型:
创建
Membership
的原因model 的作用是添加 Django 自动创建的默认模型默认情况下不会有的额外数据,但由于您不再使用默认模型,因此必须告诉 Django,使用through
。基本上,您保留了 Django 为 ManyToManyFields 提供的 API。I think you're thinking a little too literally about this. Let's say you didn't use
through
:Django, behind the scenes, essentially creates the following model for you:
The reason for creating a
Membership
model is to add extra data that the default model Django automatically creates wouldn't have by default, but since you're no longer using the default, you have to tell Django that, usingthrough
. Basically, you're preserving the API Django provides for ManyToManyFields.这样做的原因是 Group 有一个用于此关系的字段,而不是必须通过其membership_set 来跟踪该关系。
如果不出意外的话,这可以使编写查询变得更简单。至少,这可以使程序员的生活更轻松,并且代码更易于阅读。充分优化的 ORM 将能够生成适当的索引来加速此类访问(除非我大错特错,django 确实做到了这一点;或者至少 South 做到了)。
The reason why one would do this is so that Group has a field for this relationship, rather than having to follow the relationship back through its membership_set.
If nothing else, this can make writing queries simpler. At the very least, this can make a programmer's life easier, and code easier to read. A sufficiently optimising ORM would be able to generate appropriate indices to speed up such access (and unless I'm very much mistaken, django does indeed do that; or at least South does).
这样,您就可以从群组中直接访问成员。您不一定想直接处理 Membership 对象(用户甚至从未看到它们)。您只需要存储一些额外信息的组。将成员身份视为个人与组之间关联的元数据。
It's so that, from Group, you can directly access the members. You don't necessarily want to deal with Membership objects directly (the user my never even see them). You just want groups with some extra information stored. Think of Membership as meta-data on the association between Person and Group.
通过表来存储关系的属性,在本例中,例如
人
加入特定组
的日期through tables are used to store properties of the relation, in this case, e.g. the date a
Person
joined a particularGroup