summaryrefslogtreecommitdiff
path: root/django/db
diff options
context:
space:
mode:
authorAnssi Kääriäinen <akaariai@gmail.com>2013-05-21 11:06:49 +0300
committerAnssi Kääriäinen <akaariai@gmail.com>2013-05-21 11:45:24 +0300
commitbac187c0d8e829fb3ca2ca82965eabbcbcb6ddd5 (patch)
tree68c928525813836104b4587cb217c45efec9f735 /django/db
parent63cab03f6ddd9faa5a770ae08791b2a70aa0a578 (diff)
[1.5.x] Fixed prefetch_related + pickle regressions
There were a couple of regressions related to field pickling. The regressions were introduced by QuerySet._known_related_objects caching. The regressions aren't present in master, the fix was likely in f403653cf146384946e5c879ad2a351768ebc226. Fixed #20157, fixed #20257. Also made QuerySets with model=None picklable.
Diffstat (limited to 'django/db')
-rw-r--r--django/db/models/query.py18
-rw-r--r--django/db/models/sql/query.py17
2 files changed, 28 insertions, 7 deletions
diff --git a/django/db/models/query.py b/django/db/models/query.py
index 292769303d..95654ee845 100644
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -68,11 +68,27 @@ class QuerySet(object):
"""
# Force the cache to be fully populated.
len(self)
-
obj_dict = self.__dict__.copy()
obj_dict['_iter'] = None
+ obj_dict['_known_related_objects'] = dict(
+ (field.name, val) for field, val in self._known_related_objects.items()
+ )
return obj_dict
+ def __setstate__(self, obj_dict):
+ model = obj_dict['model']
+ if model is None:
+ # if model is None, then self should be emptyqs and the related
+ # objects do not matter.
+ self._known_related_objects = {}
+ else:
+ opts = model._meta
+ self._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()
+ )
+ self.__dict__.update(obj_dict)
+
def __repr__(self):
data = list(self[:REPR_OUTPUT_SIZE + 1])
if len(data) > REPR_OUTPUT_SIZE:
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index 7368fed24c..390444d799 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -208,12 +208,17 @@ class Query(object):
Unpickling support.
"""
# Rebuild list of field instances
- opts = obj_dict['model']._meta
- obj_dict['select_fields'] = [
- name is not None and opts.get_field(name) or None
- for name in obj_dict['select_fields']
- ]
-
+ model = obj_dict['model']
+ if model is None:
+ # if model is None the queryset should be emptyqs. So the
+ # select_fields do not matter.
+ obj_dict['select_fields'] = []
+ else:
+ opts = model._meta
+ obj_dict['select_fields'] = [
+ name is not None and opts.get_field(name) or None
+ for name in obj_dict['select_fields']
+ ]
self.__dict__.update(obj_dict)
def prepare(self):