我在我的以下模型定义models.py
from django.db import models
class Blog(models.Model):
title = models.CharField(max_length=100)
body = models.TextField()
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
在我settings.py有
TIME_ZONE = 'UTC'
USE_TZ = True
我想获得特定时区的时间。现在,如果我称它为Blog.objects.all()所有时间的UTC格式。但我想要请求的用户时区的时间。
我知道有可用的过滤器和标签。但正如我所做的那样,rest_framework我认为我需要能够在Queryset.
有什么帮助吗?
编辑:
到目前为止,我已经编写了这样的查询集
from django.utils import timezone
from django.db import models
Blog.objects.annotate(
local_create_time = timezone.template_localtime(models.Expressionwrapper(models.F('created'), output_field=models.DateTimeField(), pytz.timezone('Europe/Madrid'))
)
它不会将数据转换Europe/Madrid为时区。但将数据输出到时UTC区。
所以我检查了timzone.template_localtime(value, use_tz=None)定义。
对我来说 use_tz 正在传递一个对象tzinfo,但是valueExpressionWrapper(F(created))