#35499 closed Bug (duplicate)
Issue with auto_now=True DateTimefield and update_fields
Reported by: | Mohamed El-Kalioby | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 4.2 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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
Note:
See TracTickets
for help on using tickets.
This is documented and has been discussed plenty already. Please take a minute to search the bug tracker before filling a new report as pointed out in the Please read this first section. Both
auto_now
andupdate_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 rely on support channels before opening a ticket.Duplicate of #22981 and #30319 which were both wont-fix'ed.
Think of it the other way around, if
auto_now
fields are automatically added toupdate_fields
how is possible to perform asave
without them being updated?Model.save(update_fields)
is a low level option that control exactly which fields should be added to theUPDATE
statement used to perform the persist the change and is aligned with howQuerySet.update
works. The framework has chosen to have a consistent behavior towards explicit update specification over preventing complete control by treating some fields different than others.