Fixed #27499 -- Made Prefetches pickle without evaluating their QuerySet.
This commit is contained in:
parent
7dd315a46f
commit
e044026dce
|
@ -1289,6 +1289,16 @@ class Prefetch(object):
|
||||||
self.queryset = queryset
|
self.queryset = queryset
|
||||||
self.to_attr = to_attr
|
self.to_attr = to_attr
|
||||||
|
|
||||||
|
def __getstate__(self):
|
||||||
|
obj_dict = self.__dict__.copy()
|
||||||
|
if self.queryset is not None:
|
||||||
|
# Prevent the QuerySet from being evaluated
|
||||||
|
obj_dict['queryset'] = self.queryset._clone(
|
||||||
|
_result_cache=[],
|
||||||
|
_prefetch_done=True,
|
||||||
|
)
|
||||||
|
return obj_dict
|
||||||
|
|
||||||
def add_prefix(self, prefix):
|
def add_prefix(self, prefix):
|
||||||
self.prefetch_through = LOOKUP_SEP.join([prefix, self.prefetch_through])
|
self.prefetch_through = LOOKUP_SEP.join([prefix, self.prefetch_through])
|
||||||
self.prefetch_to = LOOKUP_SEP.join([prefix, self.prefetch_to])
|
self.prefetch_to = LOOKUP_SEP.join([prefix, self.prefetch_to])
|
||||||
|
|
|
@ -134,6 +134,15 @@ class PickleabilityTestCase(TestCase):
|
||||||
groups2 = pickle.loads(pickle.dumps(groups))
|
groups2 = pickle.loads(pickle.dumps(groups))
|
||||||
self.assertSequenceEqual(groups2.filter(id__gte=0), [g])
|
self.assertSequenceEqual(groups2.filter(id__gte=0), [g])
|
||||||
|
|
||||||
|
def test_pickle_prefetch_queryset_not_evaluated(self):
|
||||||
|
Group.objects.create(name='foo')
|
||||||
|
groups = Group.objects.prefetch_related(
|
||||||
|
models.Prefetch('event_set', queryset=Event.objects.order_by('id'))
|
||||||
|
)
|
||||||
|
list(groups) # evaluate QuerySet
|
||||||
|
with self.assertNumQueries(0):
|
||||||
|
pickle.loads(pickle.dumps(groups))
|
||||||
|
|
||||||
def test_pickle_prefetch_related_with_m2m_and_objects_deletion(self):
|
def test_pickle_prefetch_related_with_m2m_and_objects_deletion(self):
|
||||||
"""
|
"""
|
||||||
#24831 -- Cached properties on ManyToOneRel created in QuerySet.delete()
|
#24831 -- Cached properties on ManyToOneRel created in QuerySet.delete()
|
||||||
|
|
Loading…
Reference in New Issue