I have a model:
class Zone(models.Model):
name = models.CharField(max_length=128)
users = models.ManyToManyField(User, related_name='zones', null=True, blank=True)
And I need to contruct a filter along the lines of:
u = User.objects.filter(...zones contains a particular zone...)
It has to be a filter on User and it has to be a single filter parameter. The reason for this is that I am constructing a URL querystring to filter the admin user changelist: http://myserver/admin/auth/user/?zones=3
It seems like it should be simple but my brain isn't cooperating!
Just restating what Tomasz said.
There are many examples of
FOO__in=...
style filters in the many-to-many and many-to-one tests. Here is syntax for your specific problem:The double underscore (__) syntax is used all over the place when working with querysets.
Note that if the user may be in multiple zones used in the query, you may probably want to add .distinct(). Otherwise you get one user multiple times: