Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #21102 -- pickling a QuerySet with prefetches twice

Fixed the bug that a QuerySet that prefetches related objects cannot be
pickled and unpickled more than once (The second pickling attempt
raises an exception).

Added a new test for the queryset pickling idempotency.

The bug was introduced by
bac187c.
  • Loading branch information...
commit e66fe357b2324f984e91392286b3b0e6b5dd627e 1 parent dbc2e8e
@mjipeo mjipeo authored akaariai committed
View
4 django/db/models/query.py
@@ -80,10 +80,10 @@ def __setstate__(self, obj_dict):
if model is None:
# if model is None, then self should be emptyqs and the related
# objects do not matter.
- self._known_related_objects = {}
+ obj_dict['_known_related_objects'] = {}
else:
opts = model._meta
- self._known_related_objects = dict(
+ obj_dict['_known_related_objects'] = dict(
(opts.get_field(field.name if hasattr(field, 'name') else field), val)
for field, val in obj_dict['_known_related_objects'].items()
)
View
12 tests/regressiontests/queryset_pickle/tests.py
@@ -73,3 +73,15 @@ def test_pickle_emptyqs(self):
empty = pickle.loads(dumped)
self.assertQuerysetEqual(
empty, [])
+
+ def test_pickle_prefetch_related_idempotence(self):
+ p = Post.objects.create()
+ posts = Post.objects.prefetch_related('materials')
+
+ # First pickling
+ posts = pickle.loads(pickle.dumps(posts))
+ self.assertQuerysetEqual(posts, [p], lambda x: x)
+
+ # Second pickling
+ posts = pickle.loads(pickle.dumps(posts))
+ self.assertQuerysetEqual(posts, [p], lambda x: x)
Please sign in to comment.
Something went wrong with that request. Please try again.