Re: Filtering on Many2Many Related Objects

2011-05-29 Thread Daniel Watkins
On Sun, May 22, 2011 at 05:42:00PM -0700, Chris Beaven wrote:
> Isn't the first suggestion (__contains) achievable already by just chaining 
> two filters: Group.objects.filter(persons=p1).filter(persons=p2) ?

Sure, but if I want to filter on several different numbers of persons,
it gets very messy very quickly.  The second suggestion could also be
implemented using a count, but again that's rather unpleasant in all but
the simplest use cases.


Dan

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.



Re: Filtering on Many2Many Related Objects

2011-05-22 Thread Chris Beaven
Isn't the first suggestion (__contains) achievable already by just chaining 
two filters: Group.objects.filter(persons=p1).filter(persons=p2) ?

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.



Filtering on Many2Many Related Objects

2011-05-20 Thread Daniel Watkins
Hello all,

Earlier I was trying to filter for objects that had a set of other
objects related to them by a ManyToManyField.  It's a bit awkward, and
I have a couple of proposals to improve it.

Consider the following models:
  class Person(models.Model):
name = models.CharField(max_length=128)

  class Group(models.Model):
persons = models.ManyToManyField(Person)

Consider a group g containing Persons p1 and p2.  In order to filter
for groups containing p1 and p2, one has to construct a QuerySet
containing p1 and p2 and then pass that into the filter call.

I think a better syntax for this would be:
  Group.objects.filter(persons__contains=[p1,p2])

The second problem I hit was if I wanted to filter to get Groups which
_only_ containing p1 and p2 then I have to do something like:
  
Group.objects.annotate(num_persons=Count('persons')).filter(num_persons=2).filter(persons=q)
where q is the QuerySet mentioned earlier.

This would be considerably nicer:
  Group.objects.filter(persons=[p1,p2])

What do people think about this?  Is it worth my looking into some code
to do it?


Cheers,

Dan

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.