When you say 'filter' do you mean that you want some objects removed (i.e. filtered), or, do you mean that you want all the A's returned to be in a list that is sorted?
On Fri, Jun 24, 2011 at 5:55 AM, Tony <tonyl7...@gmail.com> wrote: > Nikhil, > This email will be significantly shorter than the one I wrote 5 > minutes ago before my laptop ran out of battery and failed to get > sent. I didn't explain this clearly so I am going to give an example > (a much smaller one than the one I jsut wrote). > Lets say I have 3 object As (a1, a2, a3) and 3 object Bs (b1, b2, b3). > the relations are as follows: > a1 to (b1, b2), a2 to (b2, b3), a3 to (b1, b3) > The user who makes the request is b2. b2 has a "friend connection" > with b1 only (so b2 to (b1,)). It could have more friends, but for > simplicity it will have only 1. What I want to return is this: > a1 to (b1,), a2 to None, a3 to (b1,). > > I always want to return all of my model "A" objects, but I want to > filter the model "B" objects within each based on who the current > user's "friend connections are". How do I do this in code? > > On Jun 23, 9:02 pm, Nikhil Somaru <nsom...@gmail.com> wrote: > > Hi Tony, > > > > 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 <tonyl7...@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+unsubscr...@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+unsubscr...@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+unsubscr...@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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.