chained many-to-manys

1 view
Skip to first unread message

Jeff Gentry

unread,
Apr 14, 2008, 1:31:32 PM4/14/08
to django...@googlegroups.com
I have a setup that I figured I could just crib off of the
User/Group/Permission code because the setup is basically identical, but
falling a bit short here (perhaps I'm just not looking at the right code).

I have three classes, As, Bs & Cs.

A has a M2M relationship with both B & C.
B has a M2M relationship with C.

For any given instance of A, I want to see all of its C's - either
directly via its M2M or inherited via its Bs. You can see why I said this
is basically identical to the user/group/permission setup.

I looked at django.contrib.auth.models and Group has a M2M
"permissions" and User has a M2M user_permissions, but all of the code
that I found referencing a user's effective permission set is going out to
the auth backends which I'm sure is not really what I want to do here. Is
there another place to look to find the best way to get what I want?

Richard Dahl

unread,
Apr 14, 2008, 3:28:38 PM4/14/08
to django...@googlegroups.com
 How about:
 
a = A.objects.get(pk=1)
c = C.objects.filter(Q(a__exact = a) | Q(b__a__exact = a)).distinct()
hth,
-richard
Reply all
Reply to author
Forward
0 new messages