为什么要用“通过” Django 模型中 ManyToManyField 的参数?

发布于 2024-12-26 05:35:15 字数 673 浏览 1 评论 0原文

查看 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

薄情伤 2025-01-02 05:35:15

我认为你对这个问题的思考有点过于字面意思了。假设您没有使用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)

    def __unicode__(self):
        return self.name

Django 在幕后本质上为您创建了以下模型:

class GroupPerson(models.Model)
    group = models.ForeignKey(Group)
    person = models.ForeignKey(Person)

创建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:

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)

    def __unicode__(self):
        return self.name

Django, behind the scenes, essentially creates the following model for you:

class GroupPerson(models.Model)
    group = models.ForeignKey(Group)
    person = models.ForeignKey(Person)

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, using through. Basically, you're preserving the API Django provides for ManyToManyFields.

没︽人懂的悲伤 2025-01-02 05:35:15

这样做的原因是 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).

后来的我们 2025-01-02 05:35:15

这样,您就可以从群组中直接访问成员。您不一定想直接处理 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.

不如归去 2025-01-02 05:35:15

通过表来存储关系的属性,在本例中,例如加入特定的日期

through tables are used to store properties of the relation, in this case, e.g. the date a Person joined a particular Group

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文