#36202: Add examples with nested arrays/objects to JSONField docs for __contains
and __contained_by
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Type:
| Cleanup/optimization
Status: new | Component:
| Documentation
Version: 5.2 | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
With data in a JSON field like:
{{{
{"key": [1, 2]}
}}}
I was initially surprised to find that this returns a match, since I
assumed the values would be compared by equality when determining whether
the top-level contains:
{{{
.filter(field__contains={"key": [1]}) # the additional value in the array
doesn't matter
}}}
----
The [
https://docs.djangoproject.com/en/5.1/topics/db/queries/#contains
docs] say:
> The returned objects are those where the given dict of key-value pairs
are all contained in the top-level of the field.
From this sentence I assumed values were checked by equality, not
containment all the way down. Apparently this is how it is supposed to
work, see #31836. Of course strings don't behave this way, `{"key":
"value"}` does not contain `{"key": "val"}`.
This seems tricky enough to warrant a little example?
--
Ticket URL: <
https://code.djangoproject.com/ticket/36202>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.