values()默认使用tzinfo = UTC返回datetime,我可以在调用values()之前更改默认时区吗?

时间:2017-03-07 05:00:52

标签: django django-queryset

{'began_datetime': datetime.datetime(2017, 3, 7, 2, 18, 3, 644392, tzinfo=<UTC>)}

例如,values()返回类似这样的内容。但我希望它使用timezone = settings.TIME_ZONE返回日期时间。这可能吗?

1 个答案:

答案 0 :(得分:1)

当您使用timezone.now()默认情况下使用settings.py中的区域定义但您可以更改它时,请参阅有关selecting the current time zone的文档。

您有3个选择:

  • 中间件
  • 查看
  • 模板

请勿忘记安装pytz以使用midlleware或查看:pip install pytz。否则,如果你在模板中这样做:

{% load tz %}

{% timezone "Europe/Paris" %}
    Paris time: {{ value }}
{% endtimezone %}

{% timezone None %}
    Server time: {{ value }}
{% endtimezone %}
相关问题