Opened 8 years ago

Closed 8 years ago

Last modified 8 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 8 years ago.
A regression test for Django's test suite

Download all attachments as: .zip

Change History (9)

by Tim Graham, 8 years ago

Attachment: 26900-test.diff added

A regression test for Django's test suite

comment:1 by Tim Graham, 8 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Tim Graham, 8 years ago

Has patch: set

comment:3 by Simon Charette, 8 years ago

Triage Stage: AcceptedReady for checkin

comment:4 by Samuel Maudo, 8 years ago

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 by Tim Graham, 8 years ago

Patch needs improvement: set
Triage Stage: Ready for checkinAccepted

comment:6 by Tim Graham, 8 years ago

Patch needs improvement: unset

I've updated the patch.

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

Resolution: fixed
Status: newclosed

In 7c33aa8a:

Fixed #26900 -- Fixed crash accessing deferred FileFields.

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

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