[Django] #34214: Bug in default for TimeField when using a timezone

33 views
Skip to first unread message

Django

unread,
Dec 15, 2022, 8:22:12 PM12/15/22
to django-...@googlegroups.com
#34214: Bug in default for TimeField when using a timezone
------------------------------------------+------------------------
Reporter: PierreAnquetil | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 4.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------+------------------------
Hi !

In Python, a datetime.time instance can have a timezone.

For a Timefield model in Django, which represents a datetime.time
instance, it should be logic to use a datetime.time to set a default.

In fact, it's partially possible :

models.TimeField(null=False,blank=False,default=time(18,30,0,0,None)) or
models.TimeField(null=False,blank=False,default=time(18,30,0,0)) works

But when you want to use a timezone on your default, it does not work :
models.TimeField(null=False,blank=False,default=time(18,30,0,0,tz.gettz('Europe/Paris')))
dont works.

In Django 3.1, the error was that it was not possible to use <= between
timefieldand Datetimefield. So i guess the error was that when setting a
timezone on a time, when the default of timefield had a timezone set,
django was converting it in a datetimefield.


In Django 4.1, the error message changed : NameError : name 'tzfile' is
not defined.
In my models.py, i use :
from dateutil import tz
...
heure_midi=models.TimeField(null=False,blank=False,default=time(18,30,0,0,tz.gettz('Europe/Paris')))

It's transformed in the migration in :

import datetime
...

migrations.AddField(
model_name='restaurant',
name='heure_soir',
field=models.TimeField(default=datetime.time(18, 30,
tzinfo=tzfile('Europe/Paris'))),
),

the import datetime works, but there is no import tz or anything, and the
tz.gettz has been changed in "tzfile"

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

Django

unread,
Dec 16, 2022, 12:33:41 AM12/16/22
to django-...@googlegroups.com
#34214: Bug in default for TimeField when using a timezone
-------------------------------------+-------------------------------------
Reporter: Pierre Anquetil | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: 4.1
(models, ORM) |
Severity: Normal | Resolution: duplicate

Keywords: | Triage Stage:
| 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: => duplicate
* component: Uncategorized => Database layer (models, ORM)
* type: Uncategorized => New feature


Comment:

It's [https://docs.djangoproject.com/en/dev/topics/i18n/timezones/#naive-
and-aware-datetime-objects documented] that: ''"Django only supports naive
time objects and will raise an exception if you attempt to save an aware
time object, as a timezone for a time with no associated date does not
make sense."''

Duplicate of #18691 (see also #26183).

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

Reply all
Reply to author
Forward
0 new messages