本文介绍了通过kwargs更新模型django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何传递包含字段的dict来更新Django模型?
这不是创建一个对象,而是要更新它。
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.
示例:
obj = Object.objects.create(index=id, **fields)
推荐答案
只要PK相同,现有行将被覆盖。
As long as the PK is the same, the existing row will be overwritten.
obj = Object(index=id, **fields)
obj.save()
这篇关于通过kwargs更新模型django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!