Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django check if querysets are equals

I have this django code

q1 = MyModel.objects.all()
q2 = MyModel.objects.all()

When I try:

print(q1 == q2)

I get as a result:

False

So how can I check if two querysets results in django are equal?

like image 832
Touhami Avatar asked Jul 20 '17 14:07

Touhami


2 Answers

You can convert the querysets to lists and check whether they are equal:

list(q1) == list(q2)
like image 179
Alasdair Avatar answered Sep 26 '22 09:09

Alasdair


You can convert it to set, to check if 2 query sets have the same elements, without regard to ordering:

set(q1) == set(q2)

it will return:

True
like image 20
MHB Avatar answered Sep 23 '22 09:09

MHB