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.

Reply via email to