Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#26900 closed Bug (fixed)

Defer FileFields causes errors when accessing them

Reported by: Samuel Maudo Owned by: nobody
Component: Database layer (models, ORM) Version: 1.10
Severity: Release blocker Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The problem is in FileDescriptor.__get__(). That method obtains the file directly from instance.__dict__ but, from Django 1.10, deferred fields don't create any item in instance.__dict__, so a KeyError is raised.

Moreover, ImageFields access the file each time a model is instantiated to update dimension fields, so a KeyError is raised whenever an instance with a deferred ImageField is created.

The obvious solution is change file = instance.__dict__[self.field.name] for file = instance.__dict__.get(self.field.name) inside of FileDescriptor.__get__() but I am not sure about the consequences.

Attachments (1)

26900-test.diff (567 bytes) - added by Tim Graham 6 years ago.
A regression test for Django's test suite

Download all attachments as: .zip

Change History (9)

Changed 6 years ago by Tim Graham

Attachment: 26900-test.diff added

A regression test for Django's test suite

comment:1 Changed 6 years ago by Tim Graham

Triage Stage: UnreviewedAccepted

comment:2 Changed 6 years ago by Tim Graham

Has patch: set

comment:3 Changed 6 years ago by Simon Charette

Triage Stage: AcceptedReady for checkin

comment:4 Changed 6 years ago by Samuel Maudo

The patch has a problem with ImageFields, I thought. Since image file is requiered whenever the model is instantiated, defer a ImageField results in an extra database query for each object in the QuerySet. That could generate hundreds of database queries.

comment:5 Changed 6 years ago by Tim Graham

Patch needs improvement: set
Triage Stage: Ready for checkinAccepted

comment:6 Changed 6 years ago by Tim Graham

Patch needs improvement: unset

I've updated the patch.

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

Resolution: fixed
Status: newclosed

In 7c33aa8a:

Fixed #26900 -- Fixed crash accessing deferred FileFields.

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

In a03ac613:

[1.10.x] Fixed #26900 -- Fixed crash accessing deferred FileFields.

Backport of 7c33aa8a87d323f0e8e5368705aa8ba96f9819d0 from master

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