For exampkle I have tabel like this,
class FormSelector(models.Model):
prefs = models.JSONField(default=dict,null=True, blank=True)
items = models.JSONField(default=dict,null=True, blank=True)
then in views, I want to do like this,
json = {"prefs":[1,2],"items":[2,3,4]}
mo = FormSelector.objects.last()
for key in json: // key is string "prefs","items"
mo.{key} = di[key] // I want to do the equivalent to mo.prefs , mo.items
Is there any good method to do this?
for key, val in json.items(): setattr(mo, key, val)
Here setattr(x, 'y', z)
is equivalent to x.y = z
.