Django timezone struggles - python

I am having difficulty getting queryset results in my own timezone.
for example:
model field definition:
some_datetime_field= models.DateTimeField(null=True)
Query:
MyModel.values_list("some_datetime_field",flat=True).first()
returns
datetime.datetime(2019, 11, 5, 14, 56, 16, tzinfo=<UTC>)
instead of returning
datetime.datetime(2019, 11, 5, 6, 56, 16, tzinfo=<DstTzInfo 'America/Los_Angeles' PST-1 day, 16:00:00 STD>)
I am working with python V3.7.5, Django V2.2.7 and PostgreSQL V12 as my database.
In my setting I have:
TIME_ZONE = "America/Los_Angeles"
USE_TZ = True
USE_I18N = True
USE_L10N = True
From the documentation it says with regard to the "TIME_ZONE" settings: (https://docs.djangoproject.com/en/2.2/ref/settings/#time-zone)
When USE_TZ is True and the database supports time zones (e.g. PostgreSQL), it is an error to set this option.
so I tried to remove the TIME_ZONE from my setting:
USE_TZ = True
USE_I18N = True
USE_L10N = True
but that didnt work either, even worse, when I try to localize it with timezone.localtime(datetime_example) i get the time in Chicago time:
datetime.datetime(2019, 11, 5, 8, 56, 16, tzinfo=<DstTzInfo 'America/Chicago' CST-1 day, 18:00:00 STD>)
How can I get my query set to return DateTime in my chosen timezone instead of UTC?

Related

Django admin: datetime is displayed in UTC despite settings

I have this in the DB:
25.0|120|CashApp|0.0|0|1|2022-03-03 22:05:17.025000
This in the settings.py:
TIME_ZONE = 'US/Eastern' # = EST
USE_I18N = True
USE_L10N = True
USE_TZ = True
And this in the admin.py:
def timestamp_(self, obj):
return obj.timestamp.strftime("%d %b, %I:%M %p") # :%S
timestamp_.admin_order_field = 'timestamp'
And on the page it's displayed like this: 03 Mar, 10:05 PM
From what I understand it stores the timestamp in UTC in the DB, which is perfect, but why it displays it in UTC on the page? I've read that if I don't do activate(), the current timezone will be the same as default timezone, which should be EST in my case. What am I missing?

Django Timezone Configuration

My django project is correctly enable the timezone in settings.
However, the datetime field of Django ORM object is a naive datetime object as shown in Block 3.
The expected result should be same as the output of Block 4 without any manually conversion.
In [1]: from django.conf import settings
...: settings.USE_TZ, settings.TIME_ZONE
Out[1]: (True, 'Asia/Hong_Kong')
In [2]: from qms.models import Quota
In [3]: q = Quota.objects.get(pk=1)
...: q.create_date, q.write_date
Out[3]:
(datetime.datetime(2021, 3, 10, 17, 37, 42, 489818),
datetime.datetime(2021, 3, 10, 17, 37, 42, 489818))
In [4]: from django.utils import timezone
...: timezone.make_aware(q.create_date,timezone.utc), \
...: timezone.make_aware(q.write_date, timezone.utc)
Out[4]:
(datetime.datetime(2021, 3, 10, 17, 37, 42, 489818, tzinfo=<UTC>),
datetime.datetime(2021, 3, 10, 17, 37, 42, 489818, tzinfo=<UTC>))
Record in SQL
Column
value
id
1
create_date
2021-03-10 17:37:42.489818+00
write_date
2021-03-10 17:37:42.489818+00
name
email
Django Model Definition
class Quota(models.models):
name = models.CharField(max_length=255)
create_date = models.DateTimeField(auto_now_add=True)
write_date = models.DateTimeField(auto_now=True)
The PostgreSQL database schema and settings, Table "public.qms_quota"
Column
Type
Modifiers
id
integer
not null default nextval('qms_quota_id_seq'::regclass)
create_date
timestamp with time zone
not null
write_date
timestamp with time zone
not null
name
character varying(255)
not null
SHOW TIMEZONE;
TimeZone
----------
UTC
Questions
How can I get the timezone-aware datetime object directly without any conversion?
Or the manual conversion is expected ?
The root-cause is a bug from a connection pool library django_postgrespool2==2.0.1.
When you use the your connection engine with "django_postgrespool2", it will NOT correctly handle the timezone settings. Releated Issue
TLDR: use engine django.db.backends.postgresql
You can use django.utils.timezone.localtime to convert the datetime received from the DB to localtime:
from django.utils.timezone import localtime
q = Quota.objects.get(pk=1)
print(localtime(q.create_date), localtime(q.write_date))

How to use timedelta with timezone.now as default?

In Django models, How to increment the date field using timezone.now?
working:
end_date = models.DateTimeField(default=timezone.now() + timezone.timedelta(days=365))
Not Working
end_date = models.DateTimeField(default=timezone.now + timezone.timedelta(days=365))
I think timezone.now is a function which runs every time when the object is created. so that error occurs.
You could use a function:
def f():
return timezone.now() + timezone.timedelta(days=365)
...
end_date = models.DateTimeField(default=f)
The current time in that timezone is the added with the timedelta anytime a new end_date is created by default:
>>> from django.utils import timezone
>>> from datetime import timedelta
>>> def f():
... return timezone.now() + timezone.timedelta(days=365)
...
>>> f()
datetime.datetime(2018, 6, 25, 19, 42, 49, 761389, tzinfo=<UTC>)
>>> f()
datetime.datetime(2018, 6, 25, 19, 43, 2, 953158, tzinfo=<UTC>)
Sample run with Django:
In [1]: from testapp import models
In [2]: models.Test.objects.create().date_added
Out[2]: datetime.datetime(2018, 6, 25, 20, 5, 28, 316214, tzinfo=<UTC>)
In [3]: models.Test.objects.create().date_added
Out[3]: datetime.datetime(2018, 6, 25, 20, 5, 33, 114624, tzinfo=<UTC>)
A good approach would be to use the post_save signal. Import it with
from django.db.models.signals import post_save and then create a handler function like this:
def handler_function(sender, instance, created, **kwargs):
if sender == YourModel and created:
instance.end_date = timezone.now() + timezone.timedelta(days=365)
instance.save()
post_save.connect(handler_function, sender=YourModel)
This will work for sure, I hope this also applies to your case. Let me know if you need further help!

django postgresql - extra datetime

In my project I have object with datetime field
startdate = models.DateTimeField(default="1999-01-01 00:00:00")
I need create new object and send datetime = "2015-12-9"
calen = models.calendar()
calen.startdate = datetime.strptime(request.POST["date"], "%Y-%m-%d")
calen.save()
In this object I see
calen.startdate => datetime.datetime(2015, 12, 9, 0, 0)
all right.
in pqAdmin3, postgres DB this field = "2015-12-09 08:00:00+02"
wrong 8 hours!!!! ->6+2
When I select this object calen.startdate
datetime.datetime(2015, 12, 9, 6, 0, tzinfo=<UTC>)
extra 6:00!!!!
I tried to make a complete date, now(),but all the same is extra 6 hours
Do not use django.utils.datetime for database fields. Use timezone instead.
from django.utils import timezone
now = timezone.now()

Django - localized date and time

I'm using Django 1.6 and I have to display a date. The timezone is Guayaquil (-05:00) and I have to get the date as:
{
'fecha': partido.fecha.strftime("%d %B"),
'hora': partido.fecha.strftime("%T"),
}
Expecting: '13 Junio' and '14:00:00' respectively, since that's the time saved and in Guayaquil timezone.
However what I get is the same time in UTC and months names in english: '13 June' and '19:00:00'.
What can I do to solve this issue?
You have to configure you time zone in your Django Settings
Choices can be found Here
Check in your settings.py:
TIME_ZONE = 'America/Guayaquil'
USE_I18N = True
USE_L10N = True
USE_TZ = True
LANGUAGE_CODE = 'es'

Categories

Resources