我有一个包含事件的Django表,每个事件都用时间戳标记。出于测试目的,我输入了一些未来的日期(2020年),但不是返回两个日期(0)之间的事件计数,而是返回自DB(5)开始以来所有事件的总和。
Django请求如下:
queryset = queryset.annotate(
value=aggregate_operation(
f"{MESSAGEMODEL_ATTRIBUTE_NAME_IN_DEVICEMODEL}__press_count", filter=timestamp_filter
)
).values(DEVICE_REFERENCE_ATTRIBUTE_NAME, "value")
我打印了所有相关变量:
filter = (OR: ('messages__timestamp__gte', datetime.datetime(2020, 1, 2, 16, 9, 5, tzinfo=<django.utils.timezone.FixedOffset object at 0x7fe619ed0c18>)), ('messages__timestamp__lte', datetime.datetime(2020, 8, 30, 16, 9, 5, tzinfo=<django.utils.timezone.FixedOffset object at 0x7fe619e9bf60>)))
value = Sum(F(messages__press_count), filter=(OR: ('messages__timestamp__gte', datetime.datetime(2020, 1, 2, 16, 9, 5, tzinfo=<django.utils.timezone.FixedOffset object at 0x7fe619ed0c18>)), ('messages__timestamp__lte', datetime.datetime(2020, 8, 30, 16, 9, 5, tzinfo=<django.utils.timezone.FixedOffset object at 0x7fe619e9bf60>))))
queryset = <QuerySet [{'name': '001 - CHANTIER1 - LIVRAISONS', 'value': 5}]>
to_timestamp
和
from timestamp
timestamp_filter = Q()
if from_timestamp:
timestamp_filter |= Q(
**{f"{MESSAGEMODEL_ATTRIBUTE_NAME_IN_DEVICEMODEL}__{TIMESTAMP_FIELD_NAME}__gte": from_timestamp}
)
if to_timestamp:
timestamp_filter |= Q(
**{f"{MESSAGEMODEL_ATTRIBUTE_NAME_IN_DEVICEMODEL}__{TIMESTAMP_FIELD_NAME}__lte": to_timestamp}
)
为什么这个请求没有按预期执行?