Django 1.8 - Many-to-Many Intermediate Relationships - What is the Consequence of Where ManytoManyField Is Used?

Example "Plenty to many through relationships 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): 

Following the documentation on intermediate models , only one pair model that needs to be linked contains ManytoManyField , model First in the above example, Is this correct?

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

thanks

EDIT (I should have been more clear):

I am interested in the staging table because I will have additional data to store in relation.

When I talk about use, I do not mean the definition of models, I mean the use of relationships (otherwise I would let Django do this).

If I want all Seconds to be associated with the First, would it be the same as getting all the ManytoManyField connected to the second, or would ManytoManyField make one direction easier than the other by introducing any additional functions?

+6
source share
2 answers

There should be no difference with the operational perspective, so the only difference is the definition of the model and the things that affect it (for example, in manager classes).

You also do not have to define a class through. Django does this automatically for you, and this whole class does support a third table to track the corresponding identifiers for each related record in two other tables. You have to decide if you want to add anything to this third table, which is important.

For example, let's say you are developing a web application for a conference. They may want to store information about visitors (both individuals and companies), as well as speakers and sponsors (as well as individuals and companies). Some of your company models may 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 this is quickly becoming cumbersome, and you will have many companies that have nothing to do with sponsorship. In addition, you can track your rating / package on the website (in the end, large sponsors get more 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() 

Also note the "related_name" attribute in ManyToManyField . This means that we can access the ConferenceSegment object through the Company instance using this name:

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

Hope this helps.

+9
source

When you add many to many fields in a model, a separate table is created in the database in which the links between the two models are stored. If you do not need to store additional information in this third table, you do not need to define a model for it.

 class First(models.Model): seconds = models.ManyToManyField(Second, related_name='firsts') class Second(models.Model): pass 

I cannot think of any difference between the definition of the many to many field in the First or Second models:

 class First(models.Model): pass class Second(models.Model): firsts = models.ManyToManyField(First, related_name='seconds') 

In both cases, using the same:

 firsts = my_second.firsts seconds = my_first.seconds 
0
source

All Articles