在django中将Postgresql时间戳与sql查询中的时区进行比较

时间:2014-10-30 22:02:45

标签: sql django postgresql django-timezone

当我运行以下查询时

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)' )

我得到了

DataError: invalid input syntax for type timestamp with time zone: "timezone.now()"

我一直在与这个避风港挣扎,并且能够弄明白。任何帮助总是受到赞赏!

1 个答案:

答案 0 :(得分:1)

django期待timestamp with time zone但你通过了timestamp without time zone。您必须将timezone添加到您的时间或在USE_TZ = False中设置settings.py

您可以使用pytz添加timezone。或者用这个:

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

用您的设置时区更改timezone