How can i test for an empty queryset in Django?
Just use exists
self.assertFalse(Tag.objects.get_for_object(Animal.objects.get(pk=1)).exists())
Chris's answer works in this case. But, you can also use:
# the_list = list(some_queryset) # converting a queryset into a list
self.assertEqual(len(the_list), 0)
# to go directly from queryset:
self.assertEqual(some_queryset.count(), 0)
self.assertEqual(Tag.objects.get_for_object(Animal.objects.get(pk=1).count(), 0)
You could also use len()
if you want to enforce the queryset being evaluated as a list!
Alternately also assertQuerysetEqual
is useful, you could do a comparison with an instance 0f django.db.models.query.EmptyQuerySet
! But using count()
should be the fastest way in most cases!