Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare Postgresql timestamp to timezone in sql query in django

When I run the following query

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've been struggling with this one haven't been able to figure it out. Any help is always appreciated!

like image 571
robert Avatar asked Feb 05 '26 00:02

robert


1 Answers

django expect timestamp with time zone But you pass timestamp without time zone. you must add timezone to your time or set USE_TZ = False in your settings.py.

you can use pytz for add timezone . or use this:

timezone.now().replace(tzinfo=timezone.get_default_timezone())

for changing timezone with your setting timezone.

like image 136
Hasan Ramezani Avatar answered Feb 06 '26 14:02

Hasan Ramezani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!