Using Django 1.10's JSONField, i want to filter queryset by the json field that have a value at specific key that contains a sub string (sql like).
e.g. there is a json field link
, with the url
key. i want the objects that it's url contains .jpg
Asked
Active
Viewed 1,707 times
1

Mohsen
- 661
- 1
- 6
- 10
-
No django support afaik, the raw query you need http://stackoverflow.com/questions/28718958/postgresql-like-query-in-json-field – serg Oct 23 '16 at 05:45
1 Answers
0
If you can, I will create this with a 'get_queryset' function for the view/model:
def get_queryset(self):
queryset = Entry.objects.all()
json = params.get('json', None)
if json is not None:
""" DO STUFF HERE TO STRIP THE JSON TO THE WANTED LINK """
q = queryset.filter(json__icontaints=".jpg")
return q
read more about this subject here: https://docs.djangoproject.com/en/1.10/topics/db/queries/

idik
- 872
- 2
- 10
- 19
-
1as specified in official docs, ```contains``` has a special meaning when filtering json field, you used icontains as of normal fields – Mohsen Oct 23 '16 at 07:35
-
1This (`json__contains`, `json__icontains`) will not work for JSON fields. https://code.djangoproject.com/ticket/26511 – jhrr Jun 14 '17 at 13:34