Thanks :)
On Oct 22, 5:52 pm, Tom Evans <tevans...@googlemail.com> wrote:
> 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
-~----------~----~----~----~------~----~------~--~---