Update model django through kwargs

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

    This question is a little old, but just to bring it up to date with recent Django developments - since 1.7 there has been an update_or_create method on querysets which works similarly to get_or_create.

    In this case it could be used like:

    obj, created = Object.objects.update_or_create(index=id, defaults={**fields})
    

提交回复
热议问题