Django - Merge jsonfield's new data with the old data
So let's assume that I have a model with a jsonfield while using Postgres as a database
class Baum(models.Model):
myjson = models.JSONField(...)
Now I'd like to know what would be the best way to edit the model fields saving behaviour/interaction with the database
- myjson stores nested api responses, so dicts&lists
- When new data comes into myjson, dont delete/overwrite the old by calling save()
- -> Instead keep the old data and just add the new data, (if a func which proves that the data is new returns True)
- I need the merged data together, in one myjson field. I am thankful for tips!
You can do:
myBaum = Baum.objects.get()
myBaum.myjson |= newdata # dictionary update, Python 3.9+
myBaum.save()