Skip to content

Commit

Permalink
Fixed #24831 -- Fixed pickling queryset with prefetch_related() after…
Browse files Browse the repository at this point in the history
… deleting objects.
  • Loading branch information
coldmind authored and timgraham committed Jun 2, 2015
1 parent e1e6399 commit 2913d6b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions django/db/models/fields/related.py
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,11 @@ def __init__(self, field, to, field_name, related_name=None, related_query_name=

self.field_name = field_name

def __getstate__(self):
state = self.__dict__.copy()
state.pop('related_model', None)
return state

def get_related_field(self):
"""
Return the Field in the 'to' object to which this relationship is tied.
Expand Down
3 changes: 3 additions & 0 deletions docs/releases/1.8.3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ Bugfixes

* Fixed lack of unique constraint when changing a field from
``primary_key=True`` to ``unique=True`` (:ticket:`24893`).

* Fixed queryset pickling when using ``prefetch_related()`` after deleting
objects (:ticket:`24831`).
14 changes: 14 additions & 0 deletions tests/queryset_pickle/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,20 @@ def test_pickle_prefetch_related_idempotence(self):
groups = pickle.loads(pickle.dumps(groups))
self.assertQuerysetEqual(groups, [g], lambda x: x)

def test_pickle_prefetch_related_with_m2m_and_objects_deletion(self):
"""
#24831 -- Cached properties on ManyToOneRel created in QuerySet.delete()
caused subsequent QuerySet pickling to fail.
"""
g = Group.objects.create(name='foo')
m2m = M2MModel.objects.create()
m2m.groups.add(g)
Group.objects.all().delete()

m2ms = M2MModel.objects.prefetch_related('groups')
m2ms = pickle.loads(pickle.dumps(m2ms))
self.assertQuerysetEqual(m2ms, [m2m], lambda x: x)

def test_missing_django_version_unpickling(self):
"""
#21430 -- Verifies a warning is raised for querysets that are
Expand Down

0 comments on commit 2913d6b

Please sign in to comment.