Opened 9 years ago
Last modified 4 weeks ago
#27222 assigned New feature
Refresh fields that are expressions after Model.save()
Description (last modified by ) ¶
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.
According to the ticket's flags, the next step(s) to move this issue forward are:
- For anyone except the patch author to review the patch using the patch review checklist and either mark the ticket as "Ready for checkin" if everything looks good, or leave comments for improvement and mark the ticket as "Patch needs improvement".
Change History (11)
comment:1 by , 9 years ago
Description: | modified (diff) |
---|
comment:2 by , 9 years ago
Summary: | Refresh expressions on save → Refresh fields that are expressions after Model.save() |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:4 by , 9 years ago
comment:5 by , 9 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:8 by , 8 years ago
Patch needs improvement: | set |
---|
Simon still has concerns described on the pull request.
comment:9 by , 4 weeks ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:10 by , 4 weeks ago
Updated patch that makes use of RETURNING
on backends that support it and clear the attributes otherwise (allowing refresh_from_db
to kick in on attribute access). Still needs a few tweaks.
comment:11 by , 4 weeks ago
Patch needs improvement: | unset |
---|
Does this also fix #23386? In that case, we might close this as a duplicate.