Django 1.9 日期时间过滤器
Django 1.9 DateTime Filter
我正在尝试过滤包含以下字段的模型:
TEMP_START = models.DateTimeField(null=True)
我正在使用语法
x.filter(TEMP_START__date = datetime.datetime(2016, 3, 31, 8, 0, tzinfo=<UTC>))
但是,这 returns 多个唯一的 TEMP_START 值。我认为这是因为我的数据没有时区存储。但是,我在 settings.py 文件中设置了:
TIME_ZONE = 'UTC' # 'America/Chicago'
USE_I18N = True
USE_L10N = True
USE_TZ = True
我不确定为什么此过滤语句会返回其他唯一的日期时间对象值。我知道“__date”过滤器是 Django 1.9 的新功能。这只是一个错误吗?
__date
字段查找适用于 日期 (仅),正如其名称所示。不在日期时间对象上;时间被忽略。
documentation,特别是示例,还显示:
For datetime fields, casts the value as date. Allows chaining additional field lookups. Takes a date value.
Example:
Entry.objects.filter(pub_date__date=datetime.date(2005, 1, 1))
Entry.objects.filter(pub_date__date__gt=datetime.date(2005, 1, 1))
正如它所说:“采用日期值。”。因此 __date
与 datetime.date
对象进行比较,而不是 datetime.datetime
对象。
您的 datetime.datetime
对象显然在幕后转换为 datetime.date
对象。
使用
__contains
关键字似乎提供了最好的结果,我没有遇到任何问题。
我正在尝试过滤包含以下字段的模型:
TEMP_START = models.DateTimeField(null=True)
我正在使用语法
x.filter(TEMP_START__date = datetime.datetime(2016, 3, 31, 8, 0, tzinfo=<UTC>))
但是,这 returns 多个唯一的 TEMP_START 值。我认为这是因为我的数据没有时区存储。但是,我在 settings.py 文件中设置了:
TIME_ZONE = 'UTC' # 'America/Chicago'
USE_I18N = True
USE_L10N = True
USE_TZ = True
我不确定为什么此过滤语句会返回其他唯一的日期时间对象值。我知道“__date”过滤器是 Django 1.9 的新功能。这只是一个错误吗?
__date
字段查找适用于 日期 (仅),正如其名称所示。不在日期时间对象上;时间被忽略。
documentation,特别是示例,还显示:
For datetime fields, casts the value as date. Allows chaining additional field lookups. Takes a date value.
Example:
Entry.objects.filter(pub_date__date=datetime.date(2005, 1, 1))
Entry.objects.filter(pub_date__date__gt=datetime.date(2005, 1, 1))
正如它所说:“采用日期值。”。因此 __date
与 datetime.date
对象进行比较,而不是 datetime.datetime
对象。
您的 datetime.datetime
对象显然在幕后转换为 datetime.date
对象。
使用
__contains
关键字似乎提供了最好的结果,我没有遇到任何问题。