


How to update the values of multiple keys in a JsonField using the .update() method using Django's ORM?
I have a model field that I'm trying to update using django's .update()
method. The field is a jsonfield. I usually keep a dictionary in this area. For example{"test": "Yes", "prod": "No"}
This is the model:
class question(models.model): # {"test": "yes", "prod": "no"} extra_data = models.jsonfield(default=dict, null=true, blank=true)
I can update the keys within the dictionary using this query (which works great, btw):
Question.filter(id="123").update(meta_data=Func( F("extra_data"), Value(["test"]), Value("no", JSONField()), function="jsonb_set", ))
Now the question is, is there a way to update multiple keys (in my case two) at once using .update()
as shown in the above query?
I want to achieve this using the .update()
method instead of .save()
so that I can avoid calling the post_save signal function.
Correct Answer
Disclaimer: It will look ugly.
I've done this in the past, albeit using pure sql, not django. The idea is to call jsonb_set()
recursively. Each call sets a key.
So, in sql, to set the key {"test":"yes","prod":"no"}
You can do this:
update question set meta_data = jsonb_set(jsonb_set(extra_data, '{test}', '"yes"'::jsonb), '{prod}', '"no"'::jsonb) where id = 123;
Please note that jsonb_set has two nested calls, the innermost one uses meta_data
, and the outermost one receives the innermost result.
So the django equivalent would look like this (note, untested):
question.filter(id="123").update( meta_data=func( func( f("extra_data"), value(["test"]), value("yes", jsonfield()), function="jsonb_set", ), value(["prod"]), value("no", jsonfield()), function="jsonb_set", ))
Or you can create a recursive function that returns the mess for you, setting one item at a time:
def recursive_jsonb_set(original, **kwargs): if kwargs: key, value = kwargs.popitem() return Func( recursive_jsonb_set(original, **kwargs), Value(key.split('__')), Value(value, JSONField()), function="jsonb_set") else: return original Question.filter(id="123").update( meta_data=recursive_jsonb_set(F("extra_data"), test="yes", prod="no", other__status="done"))
Please note the '__'
function to update subkeys.
The above is the detailed content of How to update the values of multiple keys in a JsonField using the .update() method using Django's ORM?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

Using python in Linux terminal...
