#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 <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/067.9bf8d46200e052ae56b8fe20692de96e%40djangoproject.com.

Reply via email to