[Django] #30319: model.save() surprising behavior with update_fields specified and auto_now field in model

14 views
Skip to first unread message

Django

unread,
Apr 3, 2019, 9:18:17 AM4/3/19
to django-...@googlegroups.com
#30319: model.save() surprising behavior with update_fields specified and auto_now
field in model
-------------------------------------+-------------------------------------
Reporter: sebhaase | Owner: nobody
Type: | Status: new
Uncategorized |
Component: Database | Version: 2.1
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
In section "Specifying which fields to save"
([https://docs.djangoproject.com/en/dev/ref/models/instances/#specifying-
which-fields-to-save]) it reads that `update_fields` can be specified
mostly for performance benefits...

However, I found that regarding a field with `auto_now=True` there is a
different outcome.
IOW, specifying `update_fields` can be used to keep the old date in
respective "auto_now=True"-field UNLESS that field is also explicitly
given un `update_fields`

This should be more prominently stated in the documentation.

In addition I also noticed that - using deserialization - `object.save()`
gives unexpected different results for auto_now field:
- if the object-pk already exists , the field is saved as given
- but if it does not exist, the given value is ignored, and the current
time is used instead
... very strange if you ask me...

refs:
[https://stackoverflow.com/questions/17474057/enforce-auto-now-when-using-
saveupdate-fields]
[https://groups.google.com/d/msg/django-users/sqMrUlZP2-8/FczIEwwMBAAJ]

--
Ticket URL: <https://code.djangoproject.com/ticket/30319>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 3, 2019, 10:44:58 AM4/3/19
to django-...@googlegroups.com
#30319: model.save() surprising behavior with update_fields specified and auto_now
field in model
-------------------------------------+-------------------------------------
Reporter: sebhaase | Owner: nobody
Type: Uncategorized | Status: closed
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* status: new => closed
* resolution: => invalid


Comment:

It sounds quite clear for me that if you add fields with `auto_now` flag
to the list of `updated_fields` then its value will be updated. Due to the
different behavior with and without PK please check
[https://docs.djangoproject.com/en/dev/ref/models/instances/#how-django-
knows-to-update-vs-insert how-django-knows-to-update-vs-insert]. IMO
documentation doesn't require clarification here.

If you have other support questions please see
[https://code.djangoproject.com/wiki/TicketClosingReasons/UseSupportChannels
TicketClosingReasons/UseSupportChannels] for ways to get help with Django
usage (and yes I've seen that you've already posted on django-users).

--
Ticket URL: <https://code.djangoproject.com/ticket/30319#comment:1>

Django

unread,
Apr 3, 2019, 10:46:36 AM4/3/19
to django-...@googlegroups.com
#30319: model.save() surprising behavior with update_fields specified and auto_now
field in model
-------------------------------------+-------------------------------------
Reporter: sebhaase | Owner: nobody
Type: Uncategorized | Status: closed
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* resolution: invalid => wontfix


--
Ticket URL: <https://code.djangoproject.com/ticket/30319#comment:2>

Django

unread,
Apr 3, 2019, 11:31:06 AM4/3/19
to django-...@googlegroups.com
#30319: model.save() surprising behavior with update_fields specified and auto_now
field in model
-------------------------------------+-------------------------------------
Reporter: sebhaase | Owner: nobody
Type: Uncategorized | Status: closed
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by sebhaase):

I would still suggest to rephrase:
"There will be a slight performance benefit from preventing all of the
model fields from being updated in the database."
to
"There will be a slight performance benefit from preventing all of the
model fields from being updated in the database. Note that `auto_now=True`
fields will be stay untouched unless explicitly listed in
`update_fields`."

I'm sure I'm not the only who got tripped by this - otherwise the docs of
django a really superb!

--
Ticket URL: <https://code.djangoproject.com/ticket/30319#comment:3>

Django

unread,
Sep 27, 2022, 10:54:10 AM9/27/22
to django-...@googlegroups.com
#30319: model.save() surprising behavior with update_fields specified and auto_now
field in model
-------------------------------------+-------------------------------------
Reporter: sebhaase | Owner: nobody
Type: Uncategorized | Status: closed
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Nick Pope):

This behaviour is now documented, see the
[https://github.com/django/django/pull/16124 PR].

--
Ticket URL: <https://code.djangoproject.com/ticket/30319#comment:4>

Reply all
Reply to author
Forward
0 new messages