Skip to content

Conversation

priyanshsaxena
Copy link
Contributor

Specific keys can be updated within a nested JSONField by specifying
the path and new value as arguments to the update() method, just like
the syntax used in Lookups.

@priyanshsaxena
Copy link
Contributor Author

Continuing from #9715

@felixxm
Copy link
Member

felixxm commented Jul 6, 2020

@priyanshsaxena Thanks 👍. Please provide solution for all builtin backends.

@priyanshsaxena
Copy link
Contributor Author

@felixxm reading up on the history in this ticket, and related PRs. Will commit another update soon.

from django.contrib.postgres.functions import JSONBSet
from django.db.models import Value
val = JSONBSet(
field.name, Value('{%s}' % hierarchy), Value('"%s"' % val), Value(False)
Copy link
Contributor

@bpartridge bpartridge Oct 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the val argument be the following to accommodate primitive values that aren't strings, and that don't necessarily stringify into valid JSON (such as booleans)? Test coverage should likely also be expanded accordingly. We're using the following in our codebase for the third argument to jsonb_set:

Cast(Value(json.dumps(value)), output_field=JSONField())

@felixxm
Copy link
Member

felixxm commented Nov 11, 2020

Closing due to inactivity.

@felixxm felixxm closed this Nov 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants