【发布时间】:2015-02-20 08:26:37
【问题描述】:
我想执行以下代码:
Booking.objects.filter(start_ts__isnull = False, end_ts__isnull = False).extra(select = {'amount': "strftime('%s', end_ts) - strftime('%s', start_ts)"})
但是,在 shell 中,我收到以下错误:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/polesz/Projects/duckbook/venv/lib/python3.4/site-packages/django/db/models/query.py", line 835, in extra
clone.query.add_extra(select, select_params, where, params, tables, order_by)
File "/home/polesz/Projects/duckbook/venv/lib/python3.4/site-packages/django/db/models/sql/query.py", line 1744, in add_extra
entry_params.append(next(param_iter))
StopIteration
我尝试将百分号加倍 (%%s) 并用反斜杠 (\%s) 转义它们,但都没有帮助。我还查看了this answer,但该解决方案不适用于QuerySet.extra()(或者我只是错过了重点)。
解决这个问题的正确方法是什么?
编辑:我也尝试像这样使用select_params:
Booking.objects.filter(start_ts__isnull = False, end_ts__isnull = False).extra(select = {'amount': "strftime('%s', end_ts) - strftime(%s, start_ts)"}, select_params = ['%s', '%s'])
但不管使用引号,结果查询都有\'%s\',这当然会给出一个SQL错误。
【问题讨论】:
-
你知道这个问题的答案了吗?如果是,请发布此问题的正确答案。
-
还没有,但我仍然感兴趣(尽管该项目现在已经转移到 PostgreSQL)。
标签: python django sqlite python-3.x django-1.7