【发布时间】:2022-01-17 14:47:59
【问题描述】:
你好吗?我需要一些帮助。我在 Django 中工作,我需要查询 00:00 到 00:30 分钟之间的所有销售额,所以每 30 分钟我需要查看一次销售额。我正在整理类似的东西,但它不起作用。
sales = (
SalesOrder
.objects
.annotate(Sum('subtotal'))
.filter(created_at__time__range= ('00:00:00:00', ''))
.filter(created_at__time = hour_now)
)
这是一个提供正确数据的 SQL 查询。
SELECT sum(subtotal) as tot
FROM sales_salesorder ss
where extract(minute from created_at::time) between 31 and 59 and
created_at::date = now()::date and
extract(hour from created_at) = extract(hour from CURRENT_TIME);
问题是我不能离开sql查询,因为我必须通过for循环来返回数据,这就是为什么最有效的方法是使用ORM,这就是我提出问题的原因。
【问题讨论】:
-
你能澄清一下“离开 sql 查询”是什么意思吗?您是否正在迭代(循环)列表中的对象,找到了您想要的对象,并且不再希望遍历查询?
标签: python django django-rest-framework