Filter over jsonfiled with unknown keys in Django ORM
There is a model with JSONfield in Django model:
class MyClass(models.Model):
is_info = models.BooleanField()
info = models.JSONField()
The data inside table is like:
is_info | info |
---|---|
false | |
true | {'key123':{'a':'1', 'b':'2', 'search_key':'text'},'key456':{'a':'1', 'b':'2', 'search_key':'another_value'}} |
And I need to filter somehow a query set to receive a set of rows where compound key 'search_key'='text'
and not include in result values from 'info' field where 'search_key' has some other values.
My keys of 'info' field (key123, key456 and etc.) are always different and I don't know the exact value.
Please, help me!!!)
I've tried:
q = queryset.filter(info__icontains='text')
but it return for me all the field info:
{'key123':{'a':'1', 'b':'2', 'search_key':'text'},'key456':{'a':'1', 'b':'2', 'search_key':'another_value'}}
when I need to receive only:
{'key123':{'a':'1', 'b':'2', 'search_key':'text'}}