Compare Postgresql timestamp with timezone in sql query in django
When I run the following request
Viewed.objects.raw('SELECT "recently_viewed_viewed"."id"FROM "recently_viewed_viewed" WHERE NOT ("recently_viewed_viewed"."viewed_date" <= \'timezone.now()\' AND "recently_viewed_viewed"."user_id" = user_id)' )
I get
DataError: invalid input syntax for type timestamp with time zone: "timezone.now()"
I was struggling with this, couldn't figure it out. Any help is always appreciated!
+3
source to share
1 answer
django expect timestamp with time zone
But you're passing through timestamp without time zone
. you must add timezone
to your time or set USE_TZ = False
to settings.py
.
you can use pytz
to add timezone
. or use this:
timezone.now().replace(tzinfo=timezone.get_default_timezone())
to change timezone
with your time zone setting.
+1
source to share