[Django] #32320: DateField uses unlocalized function when auto_now is True

12 views
Skip to first unread message

Django

unread,
Jan 5, 2021, 1:42:12 AM1/5/21
to django-...@googlegroups.com
#32320: DateField uses unlocalized function when auto_now is True
-------------------------------------+-------------------------------------
Reporter: M1ha-Shvn | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: master
layer (models, ORM) | Keywords: auto_now DateField
Severity: Normal | USE_TZ
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Hi.

`django.db.models.fields.DateField(auto_now=True)` sets date as
`datetime.date.today()` (see
[here](https://github.com/django/django/blob/master/django/db/models/fields/__init__.py#L1246)).
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()`

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

Django

unread,
Jan 5, 2021, 2:02:42 AM1/5/21
to django-...@googlegroups.com
#32320: DateField uses unlocalized function when auto_now is True
-------------------------------------+-------------------------------------
Reporter: M1ha-Shvn | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: auto_now DateField | Triage Stage:
USE_TZ | Unreviewed
Has patch: 0 | Needs documentation: 0

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

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


Old description:

> Hi.
>
> `django.db.models.fields.DateField(auto_now=True)` sets date as
> `datetime.date.today()` (see
> [here](https://github.com/django/django/blob/master/django/db/models/fields/__init__.py#L1246)).
> 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()`

New description:

Hi.

`django.db.models.fields.DateField(auto_now=True)` sets date as
`datetime.date.today()` (see

[https://github.com/django/django/blob/master/django/db/models/fields/__init__.py#L1246
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()`

--

Comment:

This is a
[https://docs.djangoproject.com/en/3.1/ref/models/fields/#django.db.models.DateField.auto_now_add
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."''

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

Django

unread,
Jan 5, 2021, 2:07:04 AM1/5/21
to django-...@googlegroups.com
#32320: DateField uses unlocalized function when auto_now is True
-------------------------------------+-------------------------------------
Reporter: M1ha-Shvn | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: auto_now DateField | Triage Stage:
USE_TZ | Unreviewed
Has patch: 0 | Needs documentation: 0

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

Comment (by Mariusz Felisiak):

Also, unlike `DateTimeField`, `DateField` isn't timezone-aware. See a
[https://github.com/django/django/pull/4902#discussion_r33062456 pull
request thread] where the change you suggested was proposed.

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

Reply all
Reply to author
Forward
0 new messages