Last active
September 12, 2023 19:05
-
-
Save marcgibbons/22bf6a67a93908ef93bb27d18b229611 to your computer and use it in GitHub Desktop.
Expression to concatenate Django Postgres JSONField
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db.models.expressions import CombinedExpression, F, Value | |
from django.contrib.postgres.fields import JSONField | |
expression = CombinedExpression( | |
F('my_json_field'), | |
'||', | |
Value({'fizz': 'buzz'}, JSONField()) | |
) |
yes, it's just the Postgres jsonb concatenation operator (||
).
Concatenating two objects generates an object containing the union of their keys, taking the second object's value when there are duplicate keys.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does this overwrite keys that already exist in the JSON field?