1

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

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 Answers1

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
  • 1
    as 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
  • 1
    This (`json__contains`, `json__icontains`) will not work for JSON fields. https://code.djangoproject.com/ticket/26511 – jhrr Jun 14 '17 at 13:34