diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index ce3b49cd421..2577b57a33d 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -106,6 +106,8 @@ class Query(object): Pickling support. """ obj_dict = self.__dict__.copy() + obj_dict['related_select_fields'] = [] + obj_dict['related_select_cols'] = [] del obj_dict['connection'] return obj_dict diff --git a/tests/regressiontests/queries/models.py b/tests/regressiontests/queries/models.py index 51cae7cde80..3c3b8408832 100644 --- a/tests/regressiontests/queries/models.py +++ b/tests/regressiontests/queries/models.py @@ -785,6 +785,15 @@ Bug #7204, #7506 -- make sure querysets with related fields can be pickled. If this doesn't crash, it's a Good Thing. >>> out = pickle.dumps(Item.objects.all()) +We should also be able to pickle things that use select_related(). The only +tricky thing here is to ensure that we do the related selections properly after +unpickling. +>>> qs = Item.objects.select_related() +>>> query = qs.query.as_sql()[0] +>>> query2 = pickle.loads(pickle.dumps(qs.query)) +>>> query2.as_sql()[0] == query +True + Bug #7277 >>> ann1 = Annotation.objects.create(name='a1', tag=t1) >>> ann1.notes.add(n1)