[Django] #35499: Issue with auto_now=True DateTimefield and update_fields

8 views
Skip to first unread message

Django

unread,
Jun 6, 2024, 7:06:58 AM6/6/24
to django-...@googlegroups.com
#35499: Issue with auto_now=True DateTimefield and update_fields
-------------------------------------+-------------------------------------
Reporter: Mohamed | Owner: nobody
El-Kalioby |
Type: Bug | Status: new
Component: Database | Version: 4.2
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 |
-------------------------------------+-------------------------------------
Let's assume we have the following model


{{{
class Order(models.Model):
name = models.Charfield(max_length=25)
status = models.CharField(max_length=50, default="Received")
order_date = models.DateTimeField(auto_now_add=True)
last_update = models.DateTimeField(auto_now=True)
}}}


Now in one of the views, we want to update the status to "Ready for
Shipping" so we write the following


{{{
order.status = "Ready to Shipping"
order.save(update_fields=['status'])
}}}


The issue now is that the '**last_update**' field won't be updated in the
database as it was NOT included in the `update_fields`. Which I think is
dangerous as now the developer has to add the field manually, and even
there is no warning or exception that he/she didn't add last_update field
--
Ticket URL: <https://code.djangoproject.com/ticket/35499>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 6, 2024, 7:54:06 AM6/6/24
to django-...@googlegroups.com
#35499: Issue with auto_now=True DateTimefield and update_fields
-------------------------------------+-------------------------------------
Reporter: Mohamed El-Kalioby | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: duplicate
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 Simon Charette):

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

Comment:

[https://docs.djangoproject.com/en/5.0/ref/models/instances/#specifying-
which-fields-to-save This is documented] and has been discussed plenty
already. Please take [https://code.djangoproject.com/query a minute to
search the bug tracker] before filling a new report as pointed out in the
**Please read this first** [https://code.djangoproject.com/newticket
section]. Both `auto_now` and `update_fields` being options introduced in
Django more than 10 years ago it is very likely that someone else ran into
the same problem as you already. When in doubt
[https://code.djangoproject.com/newticket rely on support channels before
opening a ticket].

Duplicate of #22981 and #30319 which were both wont-fix'ed.
--
Ticket URL: <https://code.djangoproject.com/ticket/35499#comment:1>
Reply all
Reply to author
Forward
0 new messages