Update model django through kwargs

前端 未结 6 1598
名媛妹妹
名媛妹妹 2021-02-04 01:14

How can i pass a dict which contain fields to update a Django model? This is not to create an object, but to update it.

example:

obj = Object.objects.cre         


        
6条回答
  •  名媛妹妹
    2021-02-04 01:45

    def update_object(obj, **kwargs):
        for k, v in kwargs.items():
            setattr(obj, k, v)
        obj.save()
    

提交回复
热议问题