In my app users provide their timezone and saved.
So I handle timezone conversion manually
local_time = datetime.now(tz=ZoneInfo(self.time_zone))
#time_zone is "US/Pacific"
Yet still when I save into postgres it is converted back to UTC 0.
In the settings:
# TIME_ZONE = 'UTC' #commented out
USE_L10N = False
USE_TZ = False
That too did not work, so I made a middleware:
# CUSTOM TIME ZONE HANDLER
class TimezoneMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
time_zone = "US/Pacific" #will be changed to a fn call
if time_zone:
timezone.activate(pytz.timezone(time_zone))
else:
timezone.deactivate()
return self.get_response(request)
In settings:
MIDDLEWARE = [
...
'time_log_app.utils.TimezoneMiddleware',
]
This is the function that inserts the data:
def insert_data_time_log(user, data):
#this is the data obj before insertion
{
'clock_out': datetime.datetime(2021, 8, 21, 8, 16, 30, 420947, tzinfo=zoneinfo.ZoneInfo(key='US/Pacific')),
'hourly_rate': 50
}
...
TimeLog.objects.create(pk=user, **data)
The model:
class TimeLog(models.Model):
clock_in = models.DateTimeField(null=True, blank=True)
clock_out = models.DateTimeField(null=True, blank=True)
hourly_rate = models.DecimalField(max_digits=20, decimal_places=2, null=True)
employee_type = models.CharField(max_length=200, null=True)
That too is not working!!
I'm pretty much out of hope here, what I'm doing wrong??
Or I should use raw sql for this?

from datetime import datetime datetime.now() datetime.datetime(2021, 8, 21, 8, 45, 17, 189704)data? 2) What happens in...?