Try this:
q1 = A.objects.filter(B=your_b1_instance) # that gets you all A with B = your_b1_instance
q2 = A.objects.filter(B__B=your_b2_instance) #that gets you all A with B.B = your_b2_instance
result = set(q1).intersection(set(q2)) #gives you the A's that are common to both sets.
result = list(result) #convert it back to a list
There might be an easier way to do it with just the ORM, but that should work for now
On Thu, Jun 23, 2011 at 8:46 PM, Tony <tonyl7126@gmail.com> wrote:
You have the question I was asking correct, your notation was fine.
The only thing I should add is I want to return all A, but filter my
"B1"s (as you put it) for each A. I will post my models if need be,
but they are on another computer and its not convenient right now. In
the meantime, do you have any ideas for this query?
On Jun 23, 11:50 am, Nikhil Somaru <nsom...@gmail.com> wrote:
> It is very hard to read your message. Please format it appropriately next
> time. Avoid repeating variable names and mixing classes with instances.
> Could you post your models here?
>
> Are you defining the following structure:
>
> A hasMany B;
> B hasMany A;
> B hasMany B;
>
> So you want* A such that A.yourB1.yourB2 exists*? Sorry for the notation.
>
>
>
> On Thu, Jun 23, 2011 at 12:03 PM, Tony <tonyl7...@gmail.com> wrote:--
> > I have two models with a manytomany through relation (A and B). B has
> > a self referential manytomany relation (a userprofile model). How
> > could I filter objects of model B per each relationship with model A?
> > So lets say 3 arbitrary model A objects have 20 model B object
> > relations each. I want to filter the relations so when I return the
> > filtered version of model A is outputted, each object of type model A
> > returns only object Bs (the userprofiles) that are connected through
> > the self referential manytomany relationship to the userprofile (the
> > object B, sorry if I use them interchangeably but they are the same
> > thing) that is currently sending in the request. I figure out which
> > userprofile is sending the request with a unique identifier sent by
> > the user in the request (basically their primary key). Is this type
> > of filtering possible.
>
> > --
> > 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+unsubscribe@googlegroups.com.
> > For more options, visit this group at
> >http://groups.google.com/group/django-users?hl=en.
>
> --
> Yours,
> Nikhil Somaru
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
--
Yours,
Nikhil Somaru
--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment