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