Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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?

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?

Thanks

EDIT (I should have been clearer):

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

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

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?

like image 961
StringsOnFire Avatar asked Jul 27 '15 14:07

StringsOnFire


People also ask

How does Django handle many-to-many relationship?

Behind the scenes, Django creates an intermediary join table to represent the many-to-many relationship. By default, this table name is generated using the name of the many-to-many field and the name of the table for the model that contains it.

What is ManyToManyField in Django?

A ManyToMany field is used when a model needs to reference multiple instances of another model. Use cases include: A user needs to assign multiple categories to a blog post. A user wants to add multiple blog posts to a publication.

How do I get rid of many-to-many field data in Django?

ManyToMany field has some methods that you can call: add(Object) remove(Object) clear() this one is for removing all objects.

What is AutoField in Django?

According to documentation, An AutoField is an IntegerField that automatically increments according to available IDs. One usually won't need to use this directly because a primary key field will automatically be added to your model if you don't specify otherwise.


2 Answers

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):
    pass

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

In both cases usage is the same:

firsts = my_second.firsts
seconds = my_first.seconds
like image 183
nima Avatar answered Sep 22 '22 01:09

nima


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.

like image 38
JoeLinux Avatar answered Sep 19 '22 01:09

JoeLinux