[Django] #35771: Bugs: models.DateField value has not been assigned correct date value when auto_now_add is set

4 views
Skip to first unread message

Django

unread,
Sep 18, 2024, 2:18:26 AM9/18/24
to django-...@googlegroups.com
#35771: Bugs: models.DateField value has not been assigned correct date value when
auto_now_add is set
-------------------------------------+-------------------------------------
Reporter: Ambitiont109 | Type: Bug
Status: new | Component: Database
| layer (models, ORM)
Version: 5.0 | Severity: Normal
Keywords: auto_now | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
class Vote(models.Model):
class Points(models.IntegerChoices):
Top = 3
Second = 2
Third = 1

points: models.IntegerField =
models.IntegerField(choices=Points.choices, default=Points.Top)
date: models.DateField = models.DateField(auto_now_add=True)


from django.utils import timezone

timezone.now() # datetime.datetime(2024, 9, 18, 3, 7, 3, 608467,
tzinfo=datetime.timezone.utc)
timezone.now().date()# datetime.date(2024, 9, 18)

import datetime
datetime.datetime.now() #datetime.datetime(2024, 9, 17, 23, 7, 14, 337114)
My local pc timezone is UTC-4
datetime.date.today() #datetime.date(2024, 9, 17)

v = Vote(points=Vote.Points.Top)

v.save()
v.date # Expected value is datetime.date(2024, 9, 18) since I enabled
USE_TZ=True in settings.py
# current value is datetime.date(2024, 9, 17)


"""
I figured it out myself and in `pre_save` function of models.DateField
class, we should use timezone.now().date() instead of
datetime.date.today()
"""
--
Ticket URL: <https://code.djangoproject.com/ticket/35771>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 18, 2024, 2:20:35 AM9/18/24
to django-...@googlegroups.com
#35771: Bugs: models.DateField value has not been assigned correct date value when
auto_now_add is set
-------------------------------------+-------------------------------------
Reporter: Ambitiont109 | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: auto_now | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Ambitiont109:

Old description:

> class Vote(models.Model):
> class Points(models.IntegerChoices):
> Top = 3
> Second = 2
> Third = 1
>
> points: models.IntegerField =
> models.IntegerField(choices=Points.choices, default=Points.Top)
> date: models.DateField = models.DateField(auto_now_add=True)
>

> from django.utils import timezone
>
> timezone.now() # datetime.datetime(2024, 9, 18, 3, 7, 3, 608467,
> tzinfo=datetime.timezone.utc)
> timezone.now().date()# datetime.date(2024, 9, 18)
>
> import datetime
> datetime.datetime.now() #datetime.datetime(2024, 9, 17, 23, 7, 14,
> 337114) My local pc timezone is UTC-4
> datetime.date.today() #datetime.date(2024, 9, 17)
>
> v = Vote(points=Vote.Points.Top)
>
> v.save()
> v.date # Expected value is datetime.date(2024, 9, 18) since I enabled
> USE_TZ=True in settings.py
> # current value is datetime.date(2024, 9, 17)
>

> """
> I figured it out myself and in `pre_save` function of models.DateField
> class, we should use timezone.now().date() instead of
> datetime.date.today()
> """

New description:

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

Django

unread,
Sep 18, 2024, 6:11:52 AM9/18/24
to django-...@googlegroups.com
#35771: Bugs: models.DateField value has not been assigned correct date value when
auto_now_add is set
-------------------------------------+-------------------------------------
Reporter: Ambitiont109 | Owner: (none)
Type: Bug | Status: closed
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: auto_now | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

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

Comment:

Duplicate of #32320
--
Ticket URL: <https://code.djangoproject.com/ticket/35771#comment:2>
Reply all
Reply to author
Forward
0 new messages