Updating JSONField in django rest framework

2019-05-26 15:55发布

I currently became familiar with using JSONField in django rest-framework, but I could not find any straight forward way to update a key in a stored json. There are many ways to filter JSONField depends on its internal keys, but it seems that there is no way to change, update or delete a key from already stored JSONField. But it seems that postgres can do some modifications on json keys as this answer explained.

Is there any function which is able to do modifications on JSONFields. If there is not any direct command to do this, what is the best way to implement modifications of a JSONField?

Edit:

As an example if I have a model like this:

class Thing(models.Model):
    name = models.CharField()
    properties = JSONField()

And in properties I stored a json like this :

{
"color" : "red",
"size" : "large",
"cost" : 1234
}

Then I want to change the color to "green" by using django commands.

2条回答
倾城 Initia
2楼-- · 2019-05-26 16:28
thing = Thing.objects.get(name="...")
thing.properties['color'] = 'green'
thing.save()
查看更多
登录 后发表回答