djangounit-testingdjango-queryset

How can i test for an empty queryset in Django?


I'm testing a view in Django that should remove all the tags from an object. For that i use this assertion:

self.assertEqual(list(Tag.objects.get_for_object(Animal.objects.get(pk=1))),[])

That works well, as i get an empty list in return. I wrapped the Django queryset in a list to avoid this:

AssertionError: [] != []

where an empty Django queryset is compared with an empty list.

But as this is not something i like a lot, i wondered if there is a nicer way to do that test.


Solution

  • Just use exists

    self.assertFalse(Tag.objects.get_for_object(Animal.objects.get(pk=1)).exists())