Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

233
Visualizações
Django: Store gmt time in database table instead of local time

In my customer model I have two datetime fields for storing created and updated time.

datetime_created = models.DateTimeField(auto_now_add=True)
datetime_updated = models.DateTimeField(auto_now=True)

When I create a customer, Now it is storing the local time in the database, When I get this customer data through API, It is returning the datetime in gmt time. This is fine. But I want to save the gmt time in the database. I think storing local time in database is not a good practice.

Date settings in settings.py file is

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

I have already installed "pip install pytz" module and my database is postgresql.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Best practice is to save it in UTC format.

USE_TZ = True in your settings, Django stores date and time information in UTC in the database otherwise it will store naive date time

over 4 years ago · Santiago Trujillo Relatório

0

You're rigth, the best practice is to save in UTC your datetime fields, and to convert it to your convenience in your views using pytz or in your templates using tz tag.

IE, you can use pytz to convert your datetime in an specific datetime in your views

import pytz

datetime_with_new_tz = object.created_at.astimezone(pytz.timezone(YOUR_LOCAL_TIMEZONE))`

if you want to convert the timezone in your template, you can use tz

{% load tz %}
{{object.created_at|timezone:YOUR_LOCAL_TIMEZONE}}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda