小编Ale*_*lex的帖子

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

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

python django orm timezone annotate

5
推荐指数
0
解决办法
369
查看次数

标签 统计

annotate ×1

django ×1

orm ×1

python ×1

timezone ×1