Re: Advantages of using ManyToManyField(Bar, through='Foo_Bar') syntax vs not declaring a ManyToManyField at all
The way I think of the value of the "through" table approach is when you want to attach information to the relationship that does not belong on either model. I like a band/musician example. A person can be a part of many bands A band has many people so here we have a classic M2M But a person is part of a band as a "member" so you would create a "member" through table that would hold things like: instruments_played start_date end_date These are things about that person in that band that don't belong on the person or band object. You might have relationships from this member table to other objects like songs etc -Preston On Oct 22, 7:57 am, Monika Sulikwrote: > Hi, > > I was wondering what exactly are the advantages of having code like > this: > > > > class Foo (models.Model): > bar_m2m = models.ManyToManyField(Bar,through='Foo_Bar') > > class Bar (models.Model): > pass > > class Foo_Bar (models.Model): > foo = models.ForeignKey(Foo) > bar = models.ForeignKey(Bar) > x = models.IntegerField() > > > > over having the same code, but having the Foo class like so: > > > > class Foo (models.Model): > pass > > > > In both cases I can use foo.foo_bar_set and bar.foo_bar_set (assuming > foo is a Foo Object and bar is a Bar Object). Why would I need the > bar_m2m variable? > I'm probably missing something pretty obvious, but if someone could > point out the answer to me that would be great :) > > Monika --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Advantages of using ManyToManyField(Bar, through='Foo_Bar') syntax vs not declaring a ManyToManyField at all
Thanks :) On Oct 22, 5:52 pm, Tom Evanswrote: > On Thu, 2009-10-22 at 07:57 -0700, Monika Sulik wrote: > > Hi, > > > I was wondering what exactly are the advantages of having code like > > this: > > > class Foo (models.Model): > > bar_m2m = models.ManyToManyField(Bar,through='Foo_Bar') > > > class Bar (models.Model): > > pass > > > class Foo_Bar (models.Model): > > foo = models.ForeignKey(Foo) > > bar = models.ForeignKey(Bar) > > x = models.IntegerField() > > > over having the same code, but having the Foo class like so: > > > class Foo (models.Model): > > pass > > > In both cases I can use foo.foo_bar_set and bar.foo_bar_set (assuming > > foo is a Foo Object and bar is a Bar Object). Why would I need the > > bar_m2m variable? > > I'm probably missing something pretty obvious, but if someone could > > point out the answer to me that would be great :) > > > Monika > > With a many to many with a through table, you can either go directly to > the other end of the relationship, or look at the through table. Without > it, you cannot look directly at the relationship, only at the link. > > Eg, Users can be in many UserGroups and UserGroups can have many Users. > If I use a m2m relationship with a through table, I can do both these > things: > > class UserGroup(models.Model): > users=ManyToManyField(User, through='UserUserGroupPreferences') > > class UserUserGroupPreferences(models.Model): > user=models.ForeignKey('User') > usergroup=models.ForeignKey('UserGroup') > is_group_super_user=models.BooleanField(default=False) > > u=User.objects.get(...) > # I can see directly which groups a user is in > u.usergroup_set.all() > # I can also examine things about a particular user -> group association > u.userusergrouppreferences_set.filter(is_group_super_user=True) > > Without the m2m field, I wouldn't be able to go directly to the > usergroup from the user, I would only be able to go via the through > table. > > Cheers > > Tom --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Advantages of using ManyToManyField(Bar, through='Foo_Bar') syntax vs not declaring a ManyToManyField at all
On Thu, 2009-10-22 at 07:57 -0700, Monika Sulik wrote: > Hi, > > I was wondering what exactly are the advantages of having code like > this: > > >> > class Foo (models.Model): > bar_m2m = models.ManyToManyField(Bar,through='Foo_Bar') > > class Bar (models.Model): > pass > > class Foo_Bar (models.Model): > foo = models.ForeignKey(Foo) > bar = models.ForeignKey(Bar) > x = models.IntegerField() > >> > > over having the same code, but having the Foo class like so: > > >> > class Foo (models.Model): > pass > >>> > > In both cases I can use foo.foo_bar_set and bar.foo_bar_set (assuming > foo is a Foo Object and bar is a Bar Object). Why would I need the > bar_m2m variable? > I'm probably missing something pretty obvious, but if someone could > point out the answer to me that would be great :) > > Monika > With a many to many with a through table, you can either go directly to the other end of the relationship, or look at the through table. Without it, you cannot look directly at the relationship, only at the link. Eg, Users can be in many UserGroups and UserGroups can have many Users. If I use a m2m relationship with a through table, I can do both these things: class UserGroup(models.Model): users=ManyToManyField(User, through='UserUserGroupPreferences') class UserUserGroupPreferences(models.Model): user=models.ForeignKey('User') usergroup=models.ForeignKey('UserGroup') is_group_super_user=models.BooleanField(default=False) u=User.objects.get(...) # I can see directly which groups a user is in u.usergroup_set.all() # I can also examine things about a particular user -> group association u.userusergrouppreferences_set.filter(is_group_super_user=True) Without the m2m field, I wouldn't be able to go directly to the usergroup from the user, I would only be able to go via the through table. Cheers Tom --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Advantages of using ManyToManyField(Bar, through='Foo_Bar') syntax vs not declaring a ManyToManyField at all
Hi, I was wondering what exactly are the advantages of having code like this: >> class Foo (models.Model): bar_m2m = models.ManyToManyField(Bar,through='Foo_Bar') class Bar (models.Model): pass class Foo_Bar (models.Model): foo = models.ForeignKey(Foo) bar = models.ForeignKey(Bar) x = models.IntegerField() >> over having the same code, but having the Foo class like so: >> class Foo (models.Model): pass >>> In both cases I can use foo.foo_bar_set and bar.foo_bar_set (assuming foo is a Foo Object and bar is a Bar Object). Why would I need the bar_m2m variable? I'm probably missing something pretty obvious, but if someone could point out the answer to me that would be great :) Monika --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---