Fixed #7813 -- Allow pickling of Query classes that use select_related().

Based on a patch from Justin Bronn.

The test in this patch most likely breaks on Oracle. That's another issue.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@8053 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2008-07-22 08:23:20 +00:00
parent 83e97ecf88
commit 8745beccb1
2 changed files with 11 additions and 0 deletions

View File

@ -106,6 +106,8 @@ class Query(object):
Pickling support. Pickling support.
""" """
obj_dict = self.__dict__.copy() obj_dict = self.__dict__.copy()
obj_dict['related_select_fields'] = []
obj_dict['related_select_cols'] = []
del obj_dict['connection'] del obj_dict['connection']
return obj_dict return obj_dict

View File

@ -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. this doesn't crash, it's a Good Thing.
>>> out = pickle.dumps(Item.objects.all()) >>> 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 Bug #7277
>>> ann1 = Annotation.objects.create(name='a1', tag=t1) >>> ann1 = Annotation.objects.create(name='a1', tag=t1)
>>> ann1.notes.add(n1) >>> ann1.notes.add(n1)