Comparing Model.objects.none()

Based on my stackoverflow question I realised Model.objects.none() != Model.objets.none()

I think they should be, this would be very useful for writing test asserting stuff.

Allowing this might set an undesirable precedent that comparing querysets using == is a thing that could/should be done… Especially when one can easily materialise them and compare the results that way.

1 Like

If your use case is for testing, then you can use assertQuerySetEqual() which has the behaviour you expected.

from django.test import TestCase

from myapp.models import MyModel


class QuerySetTests(TestCase):
    def test_empty_querysets_are_equal(self):
        qs1 = MyModel.objects.none()
        qs2 = MyModel.objects.none()
        self.assertNotEqual(qs1, qs2)
        self.assertQuerySetEqual(qs1, qs2)
2 Likes

Thanks guys trying to help, but in my case I was using mock.assert_any_call which it compares the 2 empty querysets internally, that’s why I created this post.

In terms of getting your problem solved, you’ll need to create a post in the “Help with using Django” part of the forum…

As far as the “Should querysets be compared with equality operator?” question is concerned, I think you’ll find that there will be uniform opposition to doing that. :+1:

Alright, I guess I will just keep using my own hack then, cheers.