Skip to content
Advertisement

Django Queryset Filter Missing Quotes

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:

JavaScript

This returns an empty list. When I look at the SQL query it generated, I get:

JavaScript

Whereas what it should have been is this:

JavaScript

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?

Advertisement

Answer

This issue is quite interesting, it seems to happen with SQLite only. It’s known here: https://code.djangoproject.com/ticket/14091 and in the docs.

So basically the query might not be wrong, but when you get the query back with Django it looks wrong:

JavaScript

Work around: if you really think the query is wrong, then provide more quote for the list, something like:

JavaScript

I would rely on the standard one anyway, the work around above is too hackish.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement