How to perform filtering with a Django JSONField?

As per the Django JSONField docs, it explains that that the data structure matches python native format, with a slightly different approach when querying. If you know the structure of the JSON, you can also filter on keys as if they were related fields: object.filter(data__animal=”cat”) object.filter(data__name=”tom”) By array access: object.filter(data__0__animal=”cat”) Your contains example is almost … Read more

Django filter JSONField list of dicts

If you wan’t to filter your data by one of fields in your array of dicts, you can try this query: Test.objects.filter(actions__contains=[{‘fixed_key_1’: ‘foo2’}]) It will list all Test objects that have at least one object in actions field that contains key fixed_key_1 of value foo2. Also it should work for nested lookup, even if you … Read more