[Answer]-Pickling Django QuerySet

1đź‘Ť

âś…

The problem is that you are trying to compare two querysets and querysets doesn’t have a __cmp__ method defined.

So, you can compare a queryset with itself and you will get this:

>> tmp == tmp
True

This is because, as there are no __cmp__ method, == evaluates True if both objects have the same identity (the same memory address). You can read it from here

So, when you do this:

>> loads(dumps(tmp, -1)) == tmp
False

you will get a False as a result because objects have different memory addresses. If you convert queries into a “comparable” object, you can get the behaviour you want. Try with this:

>> set(loads(dumps(tmp, -1))) == set(tmp)
True

Hope it helps!

Leave a comment