I have a list and I want to filter my Queryset when any of these items is found in a foreign table's non-primary key 'test'. So I write something like this:
test_list = ['test1', 'test2', 'test3', 'test4', 'test5']
return cls.objects.filter(reduce(lambda x, y: x | y, [models.Q(next_task__test = item) for item in test_list]))[:20]
This returns an empty list. When I look at the SQL query it generated, I get:
SELECT ...
FROM ...
WHERE "job"."next_task_id" IN (test1, test2, test3, test4, test5) LIMIT 20;
Whereas what it should have been is this:
SELECT ...
FROM ...
WHERE "job"."next_task_id" IN ('test1', 'test2', 'test3', 'test4', 'test5') LIMIT 20;
Without the quotes, SQLite3 believes those are column names, and does not return anything. When I manually add the quotes and execute an SQLite3 query on the table without Django at all, I get the desired results. How do I make Django issue the query correctly?
cls.objects.filter(next_task__test__in=test_list)
? – Hieu Nguyen Aug 6 '13 at 11:14in
changes nothing. Puttingexact
makes a chain ofOR
s, but the missing quotes are still missing. Typecasting as str has no effect. – Subhamoy S. Aug 6 '13 at 12:02QuerySet.query
's generated string. The query is parametrized, so there is, in fact, no string that represents the populated SQL of what's being executed. I'm sure that the issue is something else in the query that's causing zero results. – AdamKG Aug 6 '13 at 12:36