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 documentation on intermediary models, 1 model of pair related contains manytomanyfield, model first in example above. correct?

if so, model should contain manytomanyfield field? there differences in using relationship either end depending on manytomanyfield is?

thanks

edit (i should have been clearer):

i'm interested in intermediary table because have additional data store on relationship.

when usage, don't mean defining models, mean using relationship (otherwise i'd let django it's thing).

if want seconds related first, same getting firsts related second, or manytomanyfield make 1 direction easier other introducing functionality?

there shouldn't difference operational perspective, difference in definition of model , things affect (for instance, manager classes).

you don't need define "through" class. django automatically you, , class maintain third table track respective ids each related record in 2 other tables. have decide whether want add third table important.

for instance, designing web app conference. might want store information attendees (both individuals , companies), speakers , sponsors (also individuals , companies). part of models companies might 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 gets cumbersome quickly, , you'll have lots of attending companies have nothing sponsoring. also, might want track rank/package on website (after all, bigger sponsors 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 "related_name" attribute in manytomanyfield. means can access conferencesegment object via company instance using name:

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

hope helps.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -