Author: russellm
Date: 2009-04-11 08:20:51 -0500 (Sat, 11 Apr 2009)
New Revision: 10522
Modified:
django/trunk/django/db/models/sql/query.py
django/trunk/tests/regressiontests/aggregation_regress/models.py
Log:
Fixed #10197 -- Corrected pickling of querysets when a subset of fields was
selected.
Modified: django/trunk/django/db/models/sql/query.py
===================================================================
--- django/trunk/django/db/models/sql/query.py 2009-04-11 12:09:34 UTC (rev
10521)
+++ django/trunk/django/db/models/sql/query.py 2009-04-11 13:20:51 UTC (rev
10522)
@@ -123,12 +123,18 @@
obj_dict['related_select_fields'] = []
obj_dict['related_select_cols'] = []
del obj_dict['connection']
+
+ # Fields can't be pickled, so we pickle the list of field names
instead.
+ obj_dict['select_fields'] = [f.name for f in obj_dict['select_fields']]
return obj_dict
def __setstate__(self, obj_dict):
"""
Unpickling support.
"""
+ # Rebuild list of field instances
+ obj_dict['select_fields'] = [obj_dict['model']._meta.get_field(name)
for name in obj_dict['select_fields']]
+
self.__dict__.update(obj_dict)
# XXX: Need a better solution for this when multi-db stuff is
# supported. It's the only class-reference to the module-level
Modified: django/trunk/tests/regressiontests/aggregation_regress/models.py
===================================================================
--- django/trunk/tests/regressiontests/aggregation_regress/models.py
2009-04-11 12:09:34 UTC (rev 10521)
+++ django/trunk/tests/regressiontests/aggregation_regress/models.py
2009-04-11 13:20:51 UTC (rev 10522)
@@ -1,4 +1,6 @@
# coding: utf-8
+import pickle
+
from django.db import models
from django.conf import settings
@@ -242,6 +244,19 @@
>>> Book.objects.filter(id__in=ids)
[<Book: Python Web Development with Django>]
+# Regression for #10197 -- Queries with aggregates can be pickled.
+# First check that pickling is possible at all. No crash = success
+>>> qs = Book.objects.annotate(num_authors=Count('authors'))
+>>> out = pickle.dumps(qs)
+
+# Then check that the round trip works.
+>>> query = qs.query.as_sql()[0]
+>>> select_fields = qs.query.select_fields
+>>> query2 = pickle.loads(pickle.dumps(qs))
+>>> query2.query.as_sql()[0] == query
+True
+>>> query2.query.select_fields = select_fields
+
# Regression for #10199 - Aggregate calls clone the original query so the
original query can still be used
>>> books = Book.objects.all()
>>> _ = books.aggregate(Avg('authors__age'))
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---