4

So let's say I have a Model defined as below:

from django.contrib.postgres.fields import JSONField
class Job(models.Model):
    ...
    json_data = JSONField(default=dict, blank=True)

The json_data field has a key named approval_coefficient with values ranging from 0 to 1. I want to update all the Jobs with json_data__approval_coefficient as 0.25 to 0.75 without firing any post_save Signals, in one Query. Basically, I want to use the .update() to work with JSONfield.

Job.objects.filter(json_data__approval_coefficient=0.25).update(json_data...)

This doesn't work. (There are maybe 50 more keys for each Job object in json_data field, and I don't want to touch/update them at all. I am just concerned with this specific key.)

Although, I can get all the jobs using the filter query, and loop through it and update each of the entries one by one. But, this does multiple Write queries to the DB, and also fires post_save signals, which is not what I want.

On doing some digging around, found this and this PR which was never merged in Django.

How to go about this?

Python 3.6.8 Django (2, 2, 0, 'final', 0)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.