Opened 6 years ago

Closed 6 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


Wrote a minimal test case already for tests/queryset_pickle/

def test_pickle_prefetch_related_queryset_not_evaluated(self):
        groups = Group.objects.prefetch_related(
            models.Prefetch('event_set', queryset=Event.objects.order_by('id'))
        list(groups)  # evaluate QS
        with self.assertNumQueries(0):

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 Changed 6 years ago by Adam Johnson

Type: UncategorizedBug

comment:2 Changed 6 years ago by Adam Johnson

Has patch: set

comment:3 Changed 6 years ago by Tim Graham

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

comment:4 Changed 6 years ago by Tim Graham

Triage Stage: AcceptedReady for checkin

comment:5 Changed 6 years ago by Tim Graham <timograham@…>

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