Skip to content

Instantly share code, notes, and snippets.

@marcgibbons
Last active September 12, 2023 19:05
Show Gist options
  • Save marcgibbons/22bf6a67a93908ef93bb27d18b229611 to your computer and use it in GitHub Desktop.
Save marcgibbons/22bf6a67a93908ef93bb27d18b229611 to your computer and use it in GitHub Desktop.
Expression to concatenate Django Postgres JSONField
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())
)
@viraj071
Copy link

Does this overwrite keys that already exist in the JSON field?

@nilox94
Copy link

nilox94 commented Mar 25, 2023

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