Opened 8 days ago

Last modified 6 days ago

#36202 assigned Cleanup/optimization

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

Reported by: Jacob Walls Owned by: Clifford Gama
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
Pull Requests:How to create a pull request

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?

According to the ticket's flags, the next step(s) to move this issue forward are:

  • To provide a patch by sending a pull request. Claim the ticket when you start working so that someone else doesn't duplicate effort. Before sending a pull request, review your work against the patch review checklist. Check the "Has patch" flag on the ticket after sending a pull request and include a link to the pull request in the ticket comment when making that update. The usual format is: [https://github.com/django/django/pull/#### PR].

Change History (2)

comment:1 by Sarah Boyce, 8 days ago

Triage Stage: UnreviewedAccepted

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

comment:2 by Clifford Gama, 6 days ago

Owner: set to Clifford Gama
Status: newassigned
Note: See TracTickets for help on using tickets.
Back to Top