Opened 23 hours ago

Last modified 11 hours ago

#36202 new Cleanup/optimization

Add examples with nested arrays/objects to JSONField docs for __contains and __contained_by

Reported by: Jacob Walls Owned by:
Component: Documentation Version: 5.2
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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 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?

Change History (1)

comment:1 by Sarah Boyce, 11 hours ago

Triage Stage: UnreviewedAccepted

I agree adding an example would be nice, thank you for the ticket Jacob

Note: See TracTickets for help on using tickets.
Back to Top