[Django] #27499: Pickling a QuerySet evaluates the querysets given to Prefetch in prefetch_related

4 views
Skip to first unread message

Django

unread,
Nov 16, 2016, 10:22:31 AM11/16/16
to django-...@googlegroups.com
#27499: Pickling a QuerySet evaluates the querysets given to Prefetch in
prefetch_related
-------------------------------------+-------------------------------------
Reporter: Adam | Owner: nobody
Chainz |
Type: | Status: new
Uncategorized |
Component: | Version: 1.10
Uncategorized |
Severity: Normal | Keywords: prefetch_related
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/27499>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 16, 2016, 10:29:53 AM11/16/16
to django-...@googlegroups.com
#27499: Pickling a QuerySet evaluates the querysets given to Prefetch in
prefetch_related
----------------------------------+--------------------------------------
Reporter: Adam Chainz | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 1.10
Severity: Normal | Resolution:
Keywords: prefetch_related | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------
Changes (by Adam Chainz):

* type: Uncategorized => Bug


--
Ticket URL: <https://code.djangoproject.com/ticket/27499#comment:1>

Django

unread,
Nov 16, 2016, 10:50:55 AM11/16/16
to django-...@googlegroups.com
#27499: Pickling a QuerySet evaluates the querysets given to Prefetch in
prefetch_related
----------------------------------+--------------------------------------
Reporter: Adam Chainz | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 1.10
Severity: Normal | Resolution:
Keywords: prefetch_related | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------
Changes (by Adam Chainz):

* has_patch: 0 => 1


Comment:

PR: https://github.com/django/django/pull/7567

--
Ticket URL: <https://code.djangoproject.com/ticket/27499#comment:2>

Django

unread,
Nov 22, 2016, 8:28:32 AM11/22/16
to django-...@googlegroups.com
#27499: Pickling a QuerySet evaluates the querysets given to Prefetch in
prefetch_related
-------------------------------------+-------------------------------------
Reporter: Adam Chainz | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: prefetch_related | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* component: Uncategorized => Database layer (models, ORM)
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/27499#comment:3>

Django

unread,
Nov 23, 2016, 9:48:33 AM11/23/16
to django-...@googlegroups.com
#27499: Pickling a QuerySet evaluates the querysets given to Prefetch in
prefetch_related
-------------------------------------+-------------------------------------
Reporter: Adam Chainz | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: prefetch_related | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/27499#comment:4>

Django

unread,
Nov 23, 2016, 11:26:23 AM11/23/16
to django-...@googlegroups.com
#27499: Pickling a QuerySet evaluates the querysets given to Prefetch in
prefetch_related
-------------------------------------+-------------------------------------
Reporter: Adam Chainz | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: prefetch_related | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"e044026dce063ec53c16d0f755ec75eb8c84b318" e044026d]:
{{{
#!CommitTicketReference repository=""
revision="e044026dce063ec53c16d0f755ec75eb8c84b318"
Fixed #27499 -- Made Prefetches pickle without evaluating their QuerySet.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27499#comment:5>

Reply all
Reply to author
Forward
0 new messages