[Django] #35267: DateTimeField does not seem to be translated back to the local timezone on data retrieval on Postgres

32 views
Skip to first unread message

Django

unread,
Mar 3, 2024, 11:45:12 PM3/3/24
to django-...@googlegroups.com
#35267: DateTimeField does not seem to be translated back to the local timezone on
data retrieval on Postgres
-------------------------------------+-------------------------------------
Reporter: Josh | Owner: nobody
Smeaton |
Type: | Status: new
Uncategorized |
Component: Database | Version: 5.0
layer (models, ORM) | Keywords: orm datetimefield
Severity: Normal | timezones
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
https://docs.djangoproject.com/en/4.2/topics/i18n/timezones/#postgresql

> The PostgreSQL backend stores datetimes as timestamp with time zone. In
practice, this means it converts datetimes from the connection’s time zone
to UTC on storage, and from UTC to the connection’s time zone on
retrieval.

This wording seems to suggest that Django DateTimeField will return
datetimes with the tzinfo set to `settings.TIME_ZONE`, but that's not what
I observe. All data returned is in utc.

Am I mistaking the blurb on the timezone support docs?

https://github.com/django/django/blob/f5ed4306bbfd2e5543dd02cf5a22326a29253cdf/django/db/models/fields/__init__.py#L1594
defines a `to_python` which will make a datetime aware, but that's only
used by the serialization framework.

I would expect a `from_db_value` to convert the returned value from `utc`
to the timezone defined by settings.TIME_ZONE.
--
Ticket URL: <https://code.djangoproject.com/ticket/35267>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 4, 2024, 3:37:03 AM3/4/24
to django-...@googlegroups.com
#35267: DateTimeField does not seem to be translated back to the local timezone on
data retrieval on Postgres
-------------------------------------+-------------------------------------
Reporter: Josh Smeaton | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm datetimefield | Triage Stage:
timezones | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Mariusz Felisiak):

This is not a strict duplicate of #21214, but the discussion there should
shed some light on the current behavior.
--
Ticket URL: <https://code.djangoproject.com/ticket/35267#comment:1>

Django

unread,
Mar 4, 2024, 7:19:29 AM3/4/24
to django-...@googlegroups.com
#35267: DateTimeField does not seem to be translated back to the local timezone on
data retrieval on Postgres
-------------------------------------+-------------------------------------
Reporter: Josh Smeaton | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm datetimefield | Triage Stage:
timezones | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Josh Smeaton):

Thank you!

I think the docs are ambiguous. It sounds like the configured time zone
will be used for the connection from the docs I linked, but in practice
the connection uses UTC unless a query is executed on the session? Or is
there another connection parameter that controls the session timezone?

Might be worth a docs update to clarify the current behaviour?
--
Ticket URL: <https://code.djangoproject.com/ticket/35267#comment:2>

Django

unread,
Mar 4, 2024, 7:41:45 AM3/4/24
to django-...@googlegroups.com
#35267: DateTimeField does not seem to be translated back to the local timezone on
data retrieval on Postgres
-------------------------------------+-------------------------------------
Reporter: Josh Smeaton | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm datetimefield | Triage Stage:
timezones | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* cc: Aymeric Augustin (added)

Comment:

Replying to [comment:2 Josh Smeaton]:
> Thank you!
>
> I think the docs are ambiguous. It sounds like the configured time zone
will be used for the connection from the docs I linked, but in practice
the connection uses UTC unless a query is executed on the session? Or is
there another connection parameter that controls the session timezone?
>
> Might be worth a docs update to clarify the current behaviour?

Doc clarifications are always welcome. I hope that Aymeric will chime in
;)
--
Ticket URL: <https://code.djangoproject.com/ticket/35267#comment:3>

Django

unread,
Mar 4, 2024, 5:04:22 PM3/4/24
to django-...@googlegroups.com
#35267: DateTimeField does not seem to be translated back to the local timezone on
data retrieval on Postgres
-------------------------------------+-------------------------------------
Reporter: Josh Smeaton | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm datetimefield | Triage Stage:
timezones | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Josh Smeaton):

The database connection settings have their own TIME_ZONE setting:
https://docs.djangoproject.com/en/5.0/ref/settings/#time-zone, which is
what the previous docs I was referring to must have been referencing:
https://docs.djangoproject.com/en/4.2/topics/i18n/timezones/#postgresql

It makes sense to me now.
--
Ticket URL: <https://code.djangoproject.com/ticket/35267#comment:4>

Django

unread,
Mar 4, 2024, 8:08:19 PM3/4/24
to django-...@googlegroups.com
#35267: DateTimeField does not seem to be translated back to the local timezone on
data retrieval on Postgres
-------------------------------------+-------------------------------------
Reporter: Josh Smeaton | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm datetimefield | Triage Stage:
timezones | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Natalia Bidart):

Replying to [comment:4 Josh Smeaton]:
> The database connection settings have their own TIME_ZONE setting:
https://docs.djangoproject.com/en/5.0/ref/settings/#time-zone, which is
what the previous docs I was referring to must have been referencing:
https://docs.djangoproject.com/en/4.2/topics/i18n/timezones/#postgresql
>
> It makes sense to me now.

Amazing, thank you for letting us know. Do you think the first docs you
read still need adjustment? I'm thinking that the `TIME_ZONE` setting link
in the paragraph would be better pointed to this instead:
https://docs.djangoproject.com/en/dev/ref/settings/#time-zone
--
Ticket URL: <https://code.djangoproject.com/ticket/35267#comment:5>
Reply all
Reply to author
Forward
0 new messages