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.
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
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}}