lundi 22 décembre 2014

Passing literal percent sign to QuerySet.extra()

I’d like to execute the following code:



Booking.objects.filter(start_ts__isnull = False, end_ts__isnull = False).extra(select = {'amount': "strftime('%s', end_ts) - strftime('%s', start_ts)"})


However, in the shell, I get the following error:



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


I have tried doubling the percent signs (%%s) and escaping them with a backslash (\%s), but neither helped. I also looked at this answer, but that solution doesn’t work with QuerySet.extra() (or I just miss the point).


What is the correct way to solve this?


Edit: I also tried to use select_params like this:



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'])


but regardless the usage of quote signs, the resulting query has \'%s\', which gives an SQL error, of course.


Aucun commentaire:

Enregistrer un commentaire