Opened 8 years ago

Last modified 7 years ago

#27222 new New feature

Refresh fields that are expressions after Model.save()

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

Description (last modified by holvianssi)

The use case is automatically fetching the value for expressions when saving to DB. For example:

> user = User.objects.create(username=Lower('Anssi'))
> user.username == 'anssi'
True

While the above feature can be simulated somewhat easily by calling refresh_from_db() after save, an in-built implementation has the ability to use RETURNING as an optimization. In addition, it seems that refreshing objects on save would be a nice default, though this might be a bit backwards incompatible in some cases.

Change History (8)

comment:1 by holvianssi, 8 years ago

Description: modified (diff)

comment:2 by Tim Graham, 8 years ago

Summary: Refresh expressions on saveRefresh fields that are expressions after Model.save()
Triage Stage: UnreviewedAccepted

Does this also fix #23386? In that case, we might close this as a duplicate.

comment:3 by Simon Charette, 8 years ago

Has patch: set

comment:4 by Simon Charette, 8 years ago

This might also be related to #21454 or at least to the implementation proposed in it's PR as we should really be using RETURNING on backends that support it instead of performing a second SELECT to fetch the database generated fields.

comment:5 by holvianssi, 8 years ago

I'm hesitant to go with RETURNING implementation for the first patch. The select approach is really simple, and it will be needed in any case for some backends. The RETURNING approach on the other hand will be complex, and after all it's just an optimisation.

This should almost solve #21454 with a Default expression. This would need to be assigned manually to fields pre-save. Then full solution to #21454 would be adding a bit of API to do the pre-save assignments automatically.

And yes, I believe this solves #23386.

comment:6 by Tim Graham, 8 years ago

Patch needs improvement: set

Comments for improvement on the PR.

comment:7 by Tim Graham, 7 years ago

Patch needs improvement: unset

I update the PR per my comments.

comment:8 by Tim Graham, 7 years ago

Patch needs improvement: set

Simon still has concerns described on the pull request.

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