from myapp.models import MyModel
from cPickle import *
tmp = MyModel.objects.all()[:1]
print(loads(dumps(t, -1)) == t)
#Output is "False"
In my case pickled query result differs from unpickled. I already read here:
https://docs.djangoproject.com/en/dev/ref/models/querysets/#pickling-querysets
that such operations are actually allowed. So – what am I doing wrong?
upd #1: Tried cPickle and regular Pickle – got ‘False’ from both
upd #2: Possible resolution – converting QuerySet to Python list with list(). Found it while reading these: https://docs.djangoproject.com/en/dev/ref/models/querysets/#when-querysets-are-evaluated
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:
This is because, as there are no
__cmp__method,==evaluatesTrueif both objects have the same identity (the same memory address). You can read it from hereSo, when you do this:
you will get a
Falseas 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:Hope it helps!