Django QuerySet注释。如何从F表达式中用用户的时区感知datetime?

2024-10-01 00:29:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经创建了一个django1.11。应用程序。 我有一个模型来存储用户的时间段

WeeklySchedule(models.Model):

    """Model representing a weekly schedule of user"""

    user_profile = models.ForeignKey(UserProfile)

    DAY_OF_WEEK =(
    (1,'Monday'),
    (2, 'Tuesday'),
    (3, 'Wednesday'),
    (4, 'Thursday'),
    (5, 'Friday'),
    (6, 'Saturday'),
    (7, 'Sunday'),
    )
    day_of_week = models.IntegerField(choices=DAY_OF_WEEK)

    time_from = models.TimeField()
    time_to = models.TimeField()

我有一个用户档案模型

^{pr2}$

首先,我尝试根据用户的时区获取日期时间,并在以后将所有日期时间转换为utc格式,但在使用带有F表达式的make_aware方法时遇到了一个错误:

from django.utils import timezone

WeeklySchedule.objects.annotate(dt_from_aware=timezone.make_aware(datetime.date.today() + F('time_from'))).values()


Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python3.6/site-packages/django/utils/timezone.py",     line 285, in make_aware
return timezone.localize(value, is_dst=is_dst)
File "/usr/local/lib/python3.6/site-packages/pytz/__init__.py", line 226, in localize
if dt.tzinfo is not None:
AttributeError: 'CombinedExpression' object has no attribute 'tzinfo'

Tags: ofinfrom模型makemodeltimeis