[Fixed]-DJango contains does not work on JSONField

19👍

Try this:

Customer.objects.filter(data__key1__icontains='t')

4👍

https://docs.djangoproject.com/en/1.10/ref/contrib/postgres/fields/#std:fieldlookup-hstorefield.contains

Customer.objects.filter(data__contains={'key1': 'text1'})

The returned objects are those where the given dict of key-value pairs are all contained in the field.

3👍

JSONField in Django is saved with json.dumps()

You can try:

import json
search = json.dumps({'key1': 'text1'})[1:-1] # removed { and }
Customer.objects.filter(data__contains=search)

If this code fail, you can try the next:

search = '"key1":"text1"'
Customer.objects.filter(data__contains=search)
👤Virako

Leave a comment