On Wed, Feb 11, 2009 at 9:20 PM, khsing <khsing...@gmail.com> wrote:

>
> Yes, that was I wanted. This case like I know you, but you don't know me.
>
> I notice when symmetrical argument added, a group object has
> related_name_set object, so I can write code like this.
>
> def all_group(user):
>    groups = []
>    for group in user.group_set.all():
>        if not group in groups: groups.append(group)
>        for other_group in group.group_set.all():
>            if not other_group in groups: groups.append(other_group)
>    return groups
>
> that right?
>
> Thanks.
>
>
>
> On Thu, Feb 12, 2009 at 10:08 AM, Alex Gaynor <alex.gay...@gmail.com>
> wrote:
> >
> >
> > On Wed, Feb 11, 2009 at 9:06 PM, khsing <khsing...@gmail.com> wrote:
> >>
> >> Alex, thank you very much.
> >>
> >> I find if g1 belong g2, g2 will belong g1 automatically, but that is not
> >> right.
> >>
> >> should I rewrite models like this one?
> >>
> >> groups = models.ManyToManyField('self', symmetrical=False)
> >>
> >>
> >> On Wed, Feb 11, 2009 at 10:40 PM, Alex Gaynor <alex.gay...@gmail.com>
> >> wrote:
> >> >
> >> >
> >> > On Wed, Feb 11, 2009 at 2:29 AM, khsing <khsing...@gmail.com> wrote:
> >> >>
> >> >> class user(models.Model):
> >> >>    username = models.CharField(max_length=50)
> >> >>
> >> >> class group(models.Model):
> >> >>    groupname = models.CharField(max_length=50)
> >> >>    users = models.ManyToManyField(user)
> >> >>    groups = models.ManyToManyField('self')
> >> >>
> >> >> there are my models.
> >> >>
> >> >> I have no idea to get a complete group list in such models, anyone
> >> >> give me a suggestion.
> >> >>
> >> >> example:
> >> >> user U1 belong group G1 and group G1 belong G2, I want get G1 and G2
> >> >> via
> >> >> U1.
> >> >> like this
> >> >>
> >> >> def get_all_groups(user):
> >> >>    return all_groups
> >> >>
> >> >> >>>get_all-groups(U1)
> >> >> [G1, G2]
> >> >>
> >> >> that all.
> >> >>
> >> >> Thank.
> >> >>
> >> >>
> >> >>
> >> >>
> >> >> On Tue, Feb 10, 2009 at 12:18 PM, khsing <khsing...@gmail.com>
> wrote:
> >> >> > Alex, thanks, now it work.
> >> >> >
> >> >> > now have a new problem is may a group can contain itself, this will
> >> >> > be a
> >> >> > loop.
> >> >> >
> >> >> > how to avoid this condition?
> >> >> >
> >> >> >
> >> >> >
> >> >> > On Tue, Feb 10, 2009 at 11:28 AM, Alex Gaynor <
> alex.gay...@gmail.com>
> >> >> > wrote:
> >> >> >>
> >> >> >>
> >> >> >> On Mon, Feb 9, 2009 at 10:18 PM, khsing <khsing...@gmail.com>
> wrote:
> >> >> >>>
> >> >> >>> I want design a group that can contain other groups, and one
> group
> >> >> >>> can
> >> >> >>> belong many groups.
> >> >> >>>
> >> >> >>> I write such code below, but not right.
> >> >> >>>
> >> >> >>> class Group(models.Model):
> >> >> >>>    groups = models.ManyToManyField(Group)
> >> >> >>>
> >> >> >>> any suggestion?
> >> >> >>>
> >> >> >>> or how to design such a group.
> >> >> >>>
> >> >> >>> thanks.
> >> >> >>>
> >> >> >>> --
> >> >> >>> A man live in jail and want to break.
> >> >> >>> http://blog.khsing.net
> >> >> >>>
> >> >> >>>
> >> >> >>
> >> >> >> To have a relationship with oneself you do
> >> >> >>
> >> >> >> ManyToManyField("self")
> >> >> >>
> >> >> >> --
> >> >> >> "I disapprove of what you say, but I will defend to the death your
> >> >> >> right to
> >> >> >> say it." --Voltaire
> >> >> >> "The people's good is the highest law."--Cicero
> >> >> >>
> >> >> >> >>
> >> >> >>
> >> >> >
> >> >> >
> >> >> >
> >> >> > --
> >> >> > A man live in jail and want to break.
> >> >> > http://blog.khsing.net
> >> >> >
> >> >>
> >> >>
> >> >>
> >> >> --
> >> >> A man live in jail and want to break.
> >> >> http://blog.khsing.net
> >> >>
> >> >>
> >> >
> >> > you could probably do something like:
> >> > def all_groups(user):
> >> >     groups = set()
> >> >     for group in user.groups.all():
> >> >         groups.add(group)
> >> >         for other_group in group.groups.all():
> >> >             groups.add(other_group)
> >> >     return groups
> >> >
> >> > --
> >> > "I disapprove of what you say, but I will defend to the death your
> right
> >> > to
> >> > say it." --Voltaire
> >> > "The people's good is the highest law."--Cicero
> >> >
> >> > >
> >> >
> >>
> >>
> >>
> >> --
> >> A man live in jail and want to break.
> >> http://blog.khsing.net
> >>
> >>
> >
> > Yes the symmetrical argument for many to many fields on self basically
> reads
> > "if I am friends with you, are you friends with me".  In this case it's
> an
> > ownership thing, so the answer is no, if I own you, you don't own me.
> >
> > Alex
> >
> > --
> > "I disapprove of what you say, but I will defend to the death your right
> to
> > say it." --Voltaire
> > "The people's good is the highest law."--Cicero
> >
> > >
> >
>
>
>
> --
> A man live in jail and want to break.
> http://blog.khsing.net
>
> >
>
That code does what you want, but you'll probably want to use the python set
datatype(in python 2.3 you'll need to import it) as it automatically
prevents duplicates which is does very efficiently, which a list doesn't.  I
think I used them in my original example.

Alex

-- 
"I disapprove of what you say, but I will defend to the death your right to
say it." --Voltaire
"The people's good is the highest law."--Cicero

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to