Update model django through kwargs

前端 未结 6 1589
名媛妹妹
名媛妹妹 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:36

    you can simply update using methods after filter() query

    obj = Object.objects.filter(index=id).update(**fields) # fields your object(dict) may be **kwargs
    

    if its a .get() method,

    obj = Object.objects.get(index=id)
    obj['key1'] = 'value1'
    obj.save()
    

提交回复
热议问题