Opened 4 years ago

Closed 4 years ago

#31843 closed Bug (fixed)

django.db.models.query.Row is not pickleable.

Reported by: Vitaliy Yelnik Owned by: Vitaliy Yelnik
Component: Database layer (models, ORM) Version: 3.1
Severity: Normal Keywords: pickle namedtuple values_list
Cc: Sergey Fedoseev 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 (last modified by Mariusz Felisiak)

The new named parameter of QuerySet.values_list() was released In Django 2.0 (#15648).
But resulted namedtuple-s can't be pickled:

class ModelA(models.Model):
    value = models.CharField(max_length=12)
In [12]: row = ModelA.objects.values_list('id', 'value', named=True).first()

In [14]: type(row)                                                                                                                                                                                                                           
Out[14]: django.db.models.query.Row

In [16]: pickle.dumps(row)                                                                                                                                                                                                                   
PicklingError: Can't pickle <class 'django.db.models.query.Row'>: attribute lookup Row on django.db.models.query failed

In particular, as a result, such requests do not work with cacheops package.

Change History (8)

comment:1 by Mariusz Felisiak, 4 years ago

Description: modified (diff)
Summary: PicklingError: Can't pickle <class 'django.db.models.query.Row'>: attribute lookup Row on django.db.models.query faileddjango.db.models.query.Row is not pickleable.
Triage Stage: UnreviewedAccepted

comment:2 by Davit Tovmasyan, 4 years ago

In addition to the data contained in the instances, pickle also stores a string reference to the original class. This means that Row tuple class should be in query module globals so pickle can find the reference. But besides that, it should have different class names for each model and for each list of values.

comment:3 by Vitaliy Yelnik, 4 years ago

Has patch: set
Owner: changed from nobody to Vitaliy Yelnik
Status: newassigned
Triage Stage: AcceptedReady for checkin
Version: 3.03.1

comment:4 by Mariusz Felisiak, 4 years ago

Triage Stage: Ready for checkinAccepted

You shouldn't mark your own PRs as ready for checkin.

comment:5 by Mariusz Felisiak, 4 years ago

Cc: Sergey Fedoseev added

comment:6 by Mariusz Felisiak, 4 years ago

Patch needs improvement: set

comment:7 by Mariusz Felisiak, 4 years ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:8 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In 981a072d:

Fixed #31843 -- Fixed pickling named values from QuerySet.values_list().

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