I have a JSONField, called metadata
on my model in Django.
The data in that fields might looks something like this
{
"vis": {
"plots": [
// some objects here
{
"id": 1,
"x": "foo",
"y": "bar",
"externalData": [
// some objects here
{
"x": "fa",
"y": "so",
"source": {
"name": "FINDME",
"location": "some other address"
}
},
// some more objects here
]
},
// some more objects here
],
"somethingElse": []
},
"moreStuff": {}
}
I want to be able to filter models that have "name": "FINDME"
in an externalData
object (at any index), inside a plots
object (also at any index).
I initially tried
MyModel.objects.filter(metadata__vis__plots__externalData__source__name='FINDME')
No good. Then I tried
MyModel.objects.filter(metadata__vis__plots__externalData__source__contains={'name':'FINDME'})
No good. Then I tried
MyModel.objects.filter(metadata__vis__plots__externalData__contains=[{'source': {'name':'FINDME'}}])
Still no luck. Finally, I tried
MyModel.objects.filter(metadata__vis__plots__contains=[{'externalData':[{'source': {'name': 'FINDME'}}]}])
Still no hits.
Clearly, I am doing this all wrong.
Any ideas?
EDIT: I've added some comments in the JSON to make it clear that I don't only have one object in each of my arrays. I am trying to find a 'plot' at an arbitrary index and an 'externalData' at an arbitrary index that contains that "source": {"name": "FINDME"}
.