Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#26239 closed Cleanup/optimization (fixed)

Document auto_now behavior with QuerySet.update()

Reported by: Ahmed Saidi Owned by: Duane Hilton
Component: Documentation Version: 1.8
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

I don't know if this is a bug or not, but when using update(), the DateTime fields with auto_now are not updated to the current time. The documentation of auto_now doesn't say anything about that situation.
For example:

#  This won't update `updated_at`:
Order.objects.filter(user_id=7).update(check_count=3)

# You have to explicitly include it in update()
Order.objects.filter(user_id=7).update(check_count=3, updated_at=timezone.now())

Change History (11)

comment:1 Changed 4 years ago by Tim Graham

Easy pickings: set
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

comment:2 in reply to:  description ; Changed 4 years ago by AMaini503

I will try to work on this. But should update be modified to include timestamp by itself or documentation should be altered to state that timestamp needs to be passed explicitly ?

Last edited 4 years ago by Tim Graham (previous) (diff)

comment:3 in reply to:  2 Changed 4 years ago by honsdomi

Replying to AMaini503:

Replying to boussouira:

I don't know if this is a bug or not, but when using update(), the DateTime fields with auto_now are not updated to the current time. The documentation of auto_now doesn't say anything about that situation.
For example:

#  This won't update `updated_at`:
Order.objects.filter(user_id=7).update(check_count=3)

# You have to explicitly include it in update()
Order.objects.filter(user_id=7).update(check_count=3, updated_at=timezone.now())

--> I will try to work on this. But should update be modified to include timestamp by itself or documentation should be altered to state that timestamp needs to be passed explicitly ?

Hello, can someone confirm if this requires modifying code or just documentation?

Version 0, edited 4 years ago by honsdomi (next)

comment:4 Changed 4 years ago by Ahmed Saidi

Hi, Sorry for delay in replay.
I think documentation change is better, so existing applications won't have to do any changes to upgrade.
As i understand now, you must call save() for the auto-update field to be populated, which is fine, but explicitly mentioning that in the documentation good for new users (like me).
Thank you.

comment:5 Changed 4 years ago by Duane Hilton

Owner: changed from nobody to Duane Hilton
Status: newassigned

comment:6 in reply to:  4 ; Changed 4 years ago by Duane Hilton

Replying to boussouira:
I think the documentation already specifies this: "Automatically set the field to now every time the object is saved."
https://docs.djangoproject.com/en/1.9/ref/models/fields/#django.db.models.DateField.auto_now

Does the documentation need something more?

Last edited 4 years ago by Tim Graham (previous) (diff)

comment:7 in reply to:  6 Changed 4 years ago by Ahmed Saidi

I think that is good enough.
Thank you

Last edited 4 years ago by Tim Graham (previous) (diff)

comment:8 Changed 4 years ago by Tim Graham

How about adding something like, "The field is not updated when using QuerySet.update() to update other fields."

comment:9 in reply to:  8 Changed 4 years ago by Duane Hilton

Okay, I added a note to that effect. https://github.com/django/django/pull/6281

comment:10 Changed 4 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In ca5c05d:

Fixed #26239 -- Added a note about how auto_now works with QuerySet.update().

comment:11 Changed 4 years ago by Tim Graham <timograham@…>

In 421ad283:

[1.9.x] Fixed #26239 -- Added a note about how auto_now works with QuerySet.update().

Backport of ca5c05ddbe91f4aae38c4543240dbda5f1fb1db2 from master

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