Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#26787 closed Cleanup/optimization (fixed)

Accessing a deleted model field attribute triggers a query to reload the field rather than raising AttributeError (behavior change in 1.10)

Reported by: Julien Hartmann Owned by: Tim Graham
Component: Documentation Version: 1.10
Severity: Normal 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 (last modified by Julien Hartmann)

This is a consequence of the implementation of #26207 in PR 6118

Now, all fields have a DeferredAttribute descriptor. This has the consequence that this will trigger a model reload:

obj = MyModel.objects.get(pk=1)
del obj.title
obj.title

Up to version 1.9 (included), this code would raise an AttributeError. Now it silently triggers a query to reload the field from the database.

Is this intended? If so, it should probably be added to the release notes.

Change History (9)

comment:1 by Julien Hartmann, 9 years ago

Description: modified (diff)

comment:2 by Tim Graham, 9 years ago

The description of that PR gives an example of the behavior you describe:

One can also do the following after the patch:

obj = MyModel.objects.first()
del obj.field
obj.field  # <- Loads the field from DB

so I'd say that yes, that's what Anssi intended. Whether or not there's justification to change the behavior is another question, although the old behavior seems untested and undocumented so backwards-compatibility likely isn't extremely critical.

Could you explain your use case? Are there any ways to achieve it after the change?

comment:3 by Tim Graham, 9 years ago

Summary: Access to deleted attribute triggers a queryAccessing a deleted model field attribute triggers a query to reload the field rather than raising AttributeError (behavior change in 1.10)

comment:4 by Julien Hartmann, 9 years ago

I don't have a use case.

I maintain a third-party package (django-hvad), that adds transparent model translation functionalities. To ensure behavior of hvad-enabled models is consistent with vanilla django models, I have extensive test cases in place. That's how I detected this change.

I went through the release notes and found nothing, so I decided to open a ticket. Documenting the change in behavior in the release notes is perfectly fine with me. :)

comment:5 by Tim Graham, 9 years ago

Component: Database layer (models, ORM)Documentation
Owner: changed from nobody to Tim Graham
Status: newassigned
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

comment:6 by Tim Graham, 9 years ago

Has patch: set

comment:7 by Julien Hartmann, 9 years ago

Great! So from the test case I understand it is guaranteed that only the accessed field is reloaded.

I'll update my own test case to check this as well. Support for Django v1.10 will be complete soon, thanks for being so reactive.

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

Resolution: fixed
Status: assignedclosed

In 20d1cb33:

Fixed #26787 -- Documented deleting and reloading of model instance fields.

Thanks Julien Hartmann for the report.

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

In 45a6507:

[1.10.x] Fixed #26787 -- Documented deleting and reloading of model instance fields.

Thanks Julien Hartmann for the report.

Backport of 20d1cb33c2ec1242e16c49deb88e8c70517b2d1a from master

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