Django извлекает неправильные объекты datetime (переопределен часовой пояс) из базы данных postgresPython

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Django извлекает неправильные объекты datetime (переопределен часовой пояс) из базы данных postgres

Сообщение Anonymous »


I am encountering an issue with Django, psycopg, and PostgreSQL regarding the handling of timezone-aware datetimes. Below are the details of my setup and the problem I'm facing:
Environment Library Version psycopg 3.1.18 Django 4.2.10 PostgreSQL 16.1 Issue Description
I have a Django model with a DateTimeField, which creates a timestamp with timezone column in PostgreSQL. When I create a new model instance with a timezone-aware datetime and save it to the database, the datetime is stored correctly as verified by viewing it in database viewers like pgAdmin or DataGrip.

However, when retrieving the data back in Django, the datetime appears with the wrong offset. Specifically, it seems to override the original timezone offset to 0 (UTC), while the time remains unchanged, leading to incorrect datetime values.
Example
  • Datetime passed to the model: 2024-02-18 01:46:29+01:00
  • Datetime stored in the database (viewed with DataGrip): 2024-02-18 01:46:29.000000 +01:00
  • Datetime retrieved in Django: 2024-02-18 01:46:29+00:00

This discrepancy suggests that instead of simply converting the time to UTC, Django changes the timezone to UTC but keeps the time from the UTC+1 timezone, which is incorrect.
Django Settings
I have the following settings in my settings.py:

USE_TZ = True TIME_ZONE = 'UTC' I experimented with the TIME_ZONE setting in the DATABASES variable, setting it to TIME_ZONE: Europe/Zurich, which corrected the timezone to UTC+1. However, this solution seems to adjust the timezone in which I receive the data, not the underlying issue of incorrect timezone handling.

Additional Notes When using psycopg directly (without Django), this problem does not occur, and the data is returned with the correct timezone. This suggests that the issue might be within Django's handling of timezones.

Questions Am I misunderstanding Django's timezone handling? Is there a configuration step I am missing, or is this behavior potentially a bug? I would appreciate any insights or guidance on resolving this issue.


Источник: https://stackoverflow.com/questions/780 ... tgres-data
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Часовой пояс PHP Азия/Янгон показывает неправильный часовой пояс
    Гость » » в форуме Php
    0 Ответы
    126 Просмотры
    Последнее сообщение Гость
  • Вопрос Часовой пояс MariaDB и часовой пояс приложения
    Anonymous » » в форуме JAVA
    0 Ответы
    59 Просмотры
    Последнее сообщение Anonymous
  • Вопрос Часовой пояс MariaDB и часовой пояс приложения
    Anonymous » » в форуме JAVA
    0 Ответы
    67 Просмотры
    Последнее сообщение Anonymous
  • Вопрос Часовой пояс MariaDB и часовой пояс приложения
    Anonymous » » в форуме JAVA
    0 Ответы
    42 Просмотры
    Последнее сообщение Anonymous
  • Преобразовать часовой пояс Javascript в часовой пояс.
    Anonymous » » в форуме Javascript
    0 Ответы
    36 Просмотры
    Последнее сообщение Anonymous

Вернуться в «Python»