summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMinjong Chung <mjipeo@gmail.com>2013-09-13 21:18:50 +0900
committerAnssi Kääriäinen <anssi.kaariainen@thl.fi>2013-09-14 10:03:03 +0300
commite66fe357b2324f984e91392286b3b0e6b5dd627e (patch)
tree1e1a246a38819cc87554dd501377283a7032018a
parentdbc2e8eb73ca15fd3f2b9c5ddfa69651330bcead (diff)
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 bac187c0d8e829fb3ca2ca82965eabbcbcb6ddd5.
-rw-r--r--django/db/models/query.py4
-rw-r--r--tests/regressiontests/queryset_pickle/tests.py12
2 files changed, 14 insertions, 2 deletions
diff --git a/django/db/models/query.py b/django/db/models/query.py
index cf40a268c0..c523a9d338 100644
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -80,10 +80,10 @@ class QuerySet(object):
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()
)
diff --git a/tests/regressiontests/queryset_pickle/tests.py b/tests/regressiontests/queryset_pickle/tests.py
index 19519b2db9..b1be30d91f 100644
--- a/tests/regressiontests/queryset_pickle/tests.py
+++ b/tests/regressiontests/queryset_pickle/tests.py
@@ -73,3 +73,15 @@ class PickleabilityTestCase(TestCase):
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)