i have json data like:
我有json数据,如:
{
"firstname": "nav",
"lastname": "yad",
"age": 22
}
I have defined a custom method for model Geek to save it from json object.
我为模型Geek定义了一个自定义方法,以便从json对象中保存它。
def jsonToModel(self, jsonObj):
data = json.loads(jsonObj)
self.firstname = data['firstname']
self.lastname = data['lastname']
self.age = data['age']
saving json object in model like:
在模型中保存json对象,如:
>> obj = Geek()
>> obj.jsonToModel(jsondata)
>>obj.save
now i want to update an existing model instance say , i have following json data for model instance id=1
现在我想更新一个现有的模型实例说,我有以下json数据的模型实例id = 1
{
"lastname": "kumar"
}
>> ob = Geek.objects.get(id=1)
now how can i do like following by not explicitly using fieldname (lastname).
现在,我如何通过不明确使用fieldname(lastname)来做。
>> ob.*field* = *data_for_field*
>> ob.save()
1 个解决方案
#1
10
If you are going to be turning JSON data into models across your application it would probably be better to use one of the existing solutions for this such as the wonderful Django REST Framework. It works a lot like submitting Django forms and will provide a lot of important security and cleaning boilerplate.
如果您要将JSON数据转换为整个应用程序中的模型,那么最好使用现有的解决方案之一,例如精彩的Django REST Framework。它的工作方式很像提交Django表单,并将提供许多重要的安全和清理样板。
If you are determined to roll your own implementation, you could use setattr()
. This would allow you to set an arbitrary attribute on your model, so you could do the following:
如果您决定推出自己的实现,可以使用setattr()。这将允许您在模型上设置任意属性,因此您可以执行以下操作:
json_data = {"lastname":"kumar"}
ob = Geek.objects.get(id=1)
for key, value in json_data.items():
setattr(ob, key, value)
ob.save()
However, this is a bit dangerous as it would allow any arbitrary key to be set as an attribute of your object. To avoid this you could make another method on your Geek
model:
但是,这有点危险,因为它允许将任意键设置为对象的属性。为避免这种情况,您可以在Geek模型上创建另一种方法:
def update_field(self, key, value):
# This will raise an AttributeError if the key isn't an attribute
# of your model
getattr(self, key)
setattr(self, key, value)
Then you could use:
然后你可以使用:
json_data = {"lastname":"kumar"}
ob = Geek.objects.get(id=1)
for key, value in json_data.items():
ob.update_field(key, value)
ob.save(update_fields=json_data.keys()) # This will only save the updated keys
#1
10
If you are going to be turning JSON data into models across your application it would probably be better to use one of the existing solutions for this such as the wonderful Django REST Framework. It works a lot like submitting Django forms and will provide a lot of important security and cleaning boilerplate.
如果您要将JSON数据转换为整个应用程序中的模型,那么最好使用现有的解决方案之一,例如精彩的Django REST Framework。它的工作方式很像提交Django表单,并将提供许多重要的安全和清理样板。
If you are determined to roll your own implementation, you could use setattr()
. This would allow you to set an arbitrary attribute on your model, so you could do the following:
如果您决定推出自己的实现,可以使用setattr()。这将允许您在模型上设置任意属性,因此您可以执行以下操作:
json_data = {"lastname":"kumar"}
ob = Geek.objects.get(id=1)
for key, value in json_data.items():
setattr(ob, key, value)
ob.save()
However, this is a bit dangerous as it would allow any arbitrary key to be set as an attribute of your object. To avoid this you could make another method on your Geek
model:
但是,这有点危险,因为它允许将任意键设置为对象的属性。为避免这种情况,您可以在Geek模型上创建另一种方法:
def update_field(self, key, value):
# This will raise an AttributeError if the key isn't an attribute
# of your model
getattr(self, key)
setattr(self, key, value)
Then you could use:
然后你可以使用:
json_data = {"lastname":"kumar"}
ob = Geek.objects.get(id=1)
for key, value in json_data.items():
ob.update_field(key, value)
ob.save(update_fields=json_data.keys()) # This will only save the updated keys