#26900 closed Bug (fixed)
Defer FileFields causes errors when accessing them
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.
Change History (9)
by , 9 years ago
Attachment: | 26900-test.diff added |
---|
comment:1 by , 9 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:3 by , 9 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
comment:4 by , 9 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 , 9 years ago
Patch needs improvement: | set |
---|---|
Triage Stage: | Ready for checkin → Accepted |
A regression test for Django's test suite