Search code examples
sqldjangopostgresqldjango-timezone

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!


Solution

  • 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.