Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#32320 closed Bug (invalid)

DateField uses unlocalized function when auto_now is True

Reported by: M1ha Shvn Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: auto_now DateField USE_TZ
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Mariusz Felisiak)

Hi.

django.db.models.fields.DateField(auto_now=True) sets date as datetime.date.today() (see here). This date is not localized with django and can be different from django.utils.timezone.now() used in DateTimeField, if django and local timzeones differ.

Suggestion:
Change datetime.date.today() to django.utils.timezone.now().date()

Change History (2)

comment:1 by Mariusz Felisiak, 4 years ago

Description: modified (diff)
Resolution: invalid
Status: newclosed

This is a documented behavior:

"The auto_now and auto_now_add options will always use the date in the default timezone at the moment of creation or update. If you need something different, you may want to consider using your own callable default or overriding save() instead of using auto_now or auto_now_add; or using a DateTimeField instead of a DateField and deciding how to handle the conversion from datetime to date at display time."

comment:2 by Mariusz Felisiak, 4 years ago

Also, unlike DateTimeField, DateField isn't timezone-aware. See a pull request thread where the change you suggested was proposed.

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