Opened 7 years ago

Closed 7 years ago

#27499 closed Bug (fixed)

Pickling a QuerySet evaluates the querysets given to Prefetch in prefetch_related

Reported by: Adam Johnson Owned by: nobody
Component: Database layer (models, ORM) Version: 1.10
Severity: Normal Keywords: prefetch_related
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Wrote a minimal test case already for tests/queryset_pickle/tests.py:

def test_pickle_prefetch_related_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 QS
        with self.assertNumQueries(0):
            pickle.dumps(groups)

At current doing pickle.dumps evaluates Event.objects.order_by('id') which is obviously a bad thing, especially if Event is a large table!

Found this as a kind of regression when upgrading 1.9 -> 1.10 because the prefetch_related refactoring seems to be keeping the Prefetch objects around more, but afaict it has always existed.

Change History (5)

comment:1 by Adam Johnson, 7 years ago

Type: UncategorizedBug

comment:3 by Tim Graham, 7 years ago

Component: UncategorizedDatabase layer (models, ORM)
Triage Stage: UnreviewedAccepted

comment:4 by Tim Graham, 7 years ago

Triage Stage: AcceptedReady for checkin

comment:5 by Tim Graham <timograham@…>, 7 years ago

Resolution: fixed
Status: newclosed

In e044026d:

Fixed #27499 -- Made Prefetches pickle without evaluating their QuerySet.

Note: See TracTickets for help on using tickets.
Back to Top