I'm encountering an issue with filtering JSON data stored in a Django JSONField. While attempting to filter data using the icontains lookup, it seems to treat the JSON data as a string field, and the filtering doesn't work as expected. Here's a brief overview of my setup:
Model:
I have a Django model with a JSONField like this:
from django.db import models
class ChatMessage(models.Model):
template_message = models.JSONField(null=True)
The JSON data is stored in the following format in the database:
{
"header": {
"type": "text",
"content": "Hello"
},
"body": "Hi there",
"footer": "Hello there"
}
Problem Description:
The problem I'm facing is with the filtering code. I want to filter objects based on values within the JSON data, but the icontains lookup seems to be treating it as a string field rather than a JSON field.
ChatMessage.objects.filter(
Q(template_message__header__content__icontains=search_key) |
Q(template_message__body__icontains=search_key) |
Q(template_message__footer__icontains=search_key)
)
Expected Outcome:
I expect the filtering to consider the JSON structure and filter based on the presence of search_key
within any of the JSON values, such as in the content, body, or footer fields.
Error Messages:
I haven't received any error messages, but the filtering doesn't work as expected.
Using
contains
/icontains
to filter aJSONField
's value will not give you your desired result. To achieve your desired result, you can useSubstr
orKT
(Django >= 4.2)If you are using Django >= 4.2, you can make use of
KT
as below