I am trying to create a record in my Django project with the timezone set to Europe/Rome.
When I create the model object, I use this function to set the timezone:
MyModel.objects.create(
name="trial",
date=datetime_aware("2023-08-15")
)
However, I am encountering an issue where the record is being stored with the date "2023-08-14" and the UTC timezone (GMT), despite my efforts to set it to Europe/Rome.
In order to do this, I first set the following values in my settings.py file:
USE_TZ = True
TIME_ZONE = "Europe/Rome"
Next, I wrote a function to create a datetime object with the appropriate timezone:
def datetime_aware(dt_string, timezone=settings.TIME_ZONE):
if dt_string:
dt = datetime.datetime.strptime(dt_string, "%Y-%m-%d")
dt_aware = make_aware(dt, pytz.timezone(timezone))
return dt_aware
return None
Can you help me figure out what's going wrong?
in first section it does seems like your Django project is using the default time zone, it may not work effectively even if you set time zone separately. in bellow the function you created looks ok there might be the issue with the string that you are passing here dt_string:
this might be helpful