Django 1.8-中介多对多关系-使用"ManytoManyField"的结果是什么? [英] Django 1.8 - Intermediary Many-to-Many-Through Relationship - What is the consequence of where 'ManytoManyField' is used?

查看:99
本文介绍了Django 1.8-中介多对多关系-使用"ManytoManyField"的结果是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django中的多对多关系示例:

An example Many-to-Many through relationship in Django:

class First(models.Model):
    seconds = models.ManyToManyField(Second, through='Middle')

class Middle(models.Model):
    first = models.ForeignKey(First)
    second = models.ForeignKey(Second)

class Second(models.Model):

遵循有关中介模型的文档,在上面的示例中,只有一对相关的模型包含ManytoManyField,模型First.这是正确的吗?

Following the documentation on intermediary models, only one model of the pair to be related contains the ManytoManyField, model First in the example above. Is this correct?

如果是,哪个模型应包含ManytoManyField字段?根据ManytoManyField的位置,使用两端的关系是否有任何区别?

If so, which model should contain the ManytoManyField field? Are there any differences in using the relationship from either end depending on where the ManytoManyField is?

谢谢

编辑(我应该更清楚):

我对中介"表感兴趣,因为我将在关系上存储其他数据.

I'm interested in an Intermediary table because I will have additional data to store on the relationship.

当我说用法时,我并不是要定义模型,而是要使用关系(否则,我会让Django来做).

When I say usage, I don't mean defining the models, I mean using the relationship (otherwise I'd let Django do it's thing).

如果我要使所有的Seconds与First相关,那么它是否与使所有的Firsts与Second相关的事物完全相同,还是ManytoManyField通过引入任何额外的功能使一个方向比另一个方向更容易实现? /p>

If I want all Seconds related to a First, would it be exactly the same as getting all Firsts related to a Second, or would the ManytoManyField make one direction easier to do than the other by introducing any extra functionality?

推荐答案

从操作的角度来看应该没有什么区别,因此唯一的区别在于模型的定义和影响模型的事物(例如,经理班).

There shouldn't be a difference from an operational perspective, so the only difference would be in the definition of the model and things that affect it (for instance, Manager classes).

您也不总是需要定义直通"类. Django会自动为您执行此操作,而该类真正要做的就是维护第三个表,以跟踪其他两个表中每个相关记录的相应ID.您必须决定是否要向第三张表中添加任何重要的内容.

You also don't always need to define a "through" class. Django does that automatically for you, and all that class really does is maintain a third table to track the respective IDs for each related record in the two other tables. You have to decide whether you want to add anything to that third table that is important.

例如,假设您正在为会议设计一个Web应用程序.他们可能想存储有关与会者(个人和公司)以及演讲者和赞助者(个人和公司)的信息.您的公司模型的一部分可能看起来像这样:

For instance, say you are designing a web app for a conference. They might want to store information about the attendees (both individuals and companies), as well as the speakers and sponsors (also individuals and companies). Part of your models for companies might look like this:

class Company(models.Model):
    name = models.CharField(max_length=100)
    sponsored_segment = models.ForeignKey(ConferenceSegment, null=True)

class ConferenceSegment(models.Model):
    title = models.CharField(max_length=100)

但这很快变得很麻烦,而且您会参加很多与赞助无关的公司.另外,您可能想在网站上跟踪他们的排名/套餐(毕竟,更大的赞助商会获得更大的展示位置):

But that gets cumbersome quickly, and you'll have lots of attending companies that have nothing to do with sponsoring. Also, you might want to track their rank/package on the website (after all, bigger sponsors get bigger placement):

class Company(models.Model):
    name = models.CharField(max_length=100)

class ConferenceSegment(models.Model):
    title = models.CharField(max_length=100)
    sponsors = models.ManyToManyField(Company, through=u'Sponsor', related_name=u'sponsored_segments')

class Sponsor(models.Model):
    company = models.ForeignKey(Company)
    segment = models.ForeignKey(ConferenceSegment)
    rank = models.PositiveIntegerField()

还请注意ManyToManyField中的"related_name"属性.这意味着我们可以使用该名称通过Company实例访问ConferenceSegment对象:

Notice also the "related_name" attribute in the ManyToManyField. This means that we can access the ConferenceSegment object via a Company instance by using that name:

c = Company.objects.get(...)
segments = c.sponsored_segments.all()

希望这会有所帮助.

这篇关于Django 1.8-中介多对多关系-使用"ManytoManyField"的结果是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆