Django 1.8 - Intermediary Many-to-Many-Through Relationship - What is the consequence of where 'ManytoManyField' is used? Django 1.8 - Intermediary Many-to-Many-Through Relationship - What is the consequence of where 'ManytoManyField' is used? django django

Django 1.8 - Intermediary Many-to-Many-Through Relationship - What is the consequence of where 'ManytoManyField' is used?


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).

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.

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()

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()

Hope this helps.


When you add a many to many field to a model a separate table is created in the database that stores the links between two models. If you don't need to store any extra information in this third table then you don't have to define a model for it.

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

I can't think of any difference between defining the many to many field in the First or Second models:

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

In both cases usage is the same:

firsts = my_second.firstsseconds = my_first.seconds