django - query filter on manytomany is empty

John picture John · Nov 15, 2010 · Viewed 30.3k times · Source

In Django is there a way to filter on a manytomany field being empty or null.

class TestModel(models.Model):
    name = models.CharField(_('set name'), max_length=200)
    manytomany = models.ManyToManyField('AnotherModel', blank=True, null=True)

print TestModel.objects.filter(manytomany__is_null=True)

Answer

Bernhard Vallant picture Bernhard Vallant · Nov 15, 2010
print TestModel.objects.filter(manytomany=None)