'Set Django JSONField value to a JSON null singleton with the Django ORM
Using a Django JSONField on PostgreSQL, say I have the following model
class Foo(models.Model):
bar = models.JSONField(null=False)
and I want to set a row to have bar=null where null is a single JSON null value (different to a database NULL value).
I can achieve it through a raw SQL query:
UPDATE "myapp_foo"
SET "bar" = 'null'::jsonb
WHERE "myapp_foo"."id" = <relevant_id>
The closest things I can do in the ORM is
Foo.objects.filter(id=<relevant_id>).update(bar=None)
but Django is interpreting this as a database NULL and I get the following error:
null value in column "bar" violates not-null constraint
or
Foo.objects.filter(id=<relevant_id>).update(bar="null")
but Django (correctly) interprets this as the JSON string "null"
Am I missing something or is this a gap in the ORM functionality?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
