Django QuerySet批注。如何从F表达式中识别用户时区的日期时间?

Ale*_*lex 5 python django orm timezone annotate

我创建了一个Django 1.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()
Run Code Online (Sandbox Code Playgroud)

我有一个用于用户个人资料的模型

class UserProfile(models.Model):

    # This line is required. Links UserProfile to a User model instance.
    user = models.OneToOneField(User, related_name='profile')

    # The additional attributes we wish to include.

    timezone = models.CharField(
        max_length=255,
        choices=[(t,t) for t in pytz.common_timezones],
        blank=True, null=True)
Run Code Online (Sandbox Code Playgroud)

首先,我尝试根据用户的时区了解日期时间,并稍后在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'
Run Code Online (Sandbox Code Playgroud)