I'm working with raw SQL queries in a Django+PostgreSQL app, and I've come across the problem of having to pass as parameter a PostgreSQL Array value (of type tags = ArrayField(m.CharField(max_length=80) in app / tags character varying(80)[] in db).
I'm using code like this:
Product.objects.raw('SELECT * FROM myapp_code_products WHERE tags @> %s', [
['red'] # also tried with simply string '{"red"}' but that didn't work either, also tried with tuple
])
...in the hope of getting SQL like this generated:
SELECT * FROM myapp_code_products WHERE (tags @> '{"red"}')
but I either get simply nothing substituted like (tags @>) or list or tuple substitution like (tags @> ['red']) or (tags @> ('red')) and obviously none of this result in a valid contains query.
And, if anyone is wondering, the substituted parameter is user-supplied, so just completely bypassing parameter substitution here is not an option security-wise.
Note: also, using raw queries is to be considered a requirement here, using querysets and model queries is not an option here.
(Detail: I'm using Django 1.10.5 with Python 3.6.0 and PostgreSQL 9.3 on Windows 10 64bit)
ArrayField's contains lookup which builds@>queries for you, right?