[Fixed]-Django JSONField and searching through the list of dictionaries using ILIKE

19👍

this works for me (note the [])

query = User.objects.filter(data__campaigns__contains=[{'key': 'value'}])
👤Theo

15👍

You should be able to search simply by chaining it, django style:

MyModel.objects.filter(json_data__results__contains={"comment":"text comment"})

check out the documentation for JSON field in Django 1.9:
https://docs.djangoproject.com/es/1.9/ref/contrib/postgres/fields/#querying-jsonfield

which includes contains lookup:
https://docs.djangoproject.com/es/1.9/ref/contrib/postgres/fields/#std:fieldlookup-hstorefield.contains

If this doesn’t work for case-insensitive, then I would see what query it produces, and simply rework it with extra where:

MyModel.objects.extra(where=["json_data->>'results'->'comment' ILIKE %s"], params=["%text comment%"])

or you can use the specific symbols for json as stated in postgres documentation, like <@

http://www.postgresql.org/docs/9.5/static/functions-json.html

Leave a comment