python - 像字典一样设置 Django 模型实例字段

标签 python django model

在django中,我们可以像这样更新模型实例字段:

from .models import Credentials
current_cre=Credentials.object.get(user=request.user)

current_cre.fb_name='Name'
current_cre.fb_pass='****'
current_cre.save()

有什么办法可以像字典项一样做到这一点:

current_cre['fb_name']='Name'
current_cre['fb_pass']='Pass'

所以我可以像这样迭代这个过程:

for field_name in List_Fields:
    current_cre[field_name]=request.POST[field_name]

提前致谢。

最佳答案

试试这个

List_Fields = [(f.verbose_name, f.name) for f in Credentials._meta.get_fields()]

for field_name, value in List_Fields:
    current_cre[field_name]=request.POST[field_name]

关于python - 像字典一样设置 Django 模型实例字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51567449/

相关文章:

python - 定义嵌套字典的简单方法?

python - 断言错误: View function mapping is overwriting an existing endpoint function

python - 在 Python 解释器中,返回没有 "'"

database - 如何从数据库加载 Zend Framework 应用程序详细信息

python 3 : No module named 'sklearn.model_selection'

python - 如何使用多处理在Python中运行多个异步进程?

jquery - 单击弹出窗口会滚动回页面顶部 [Bootstrap and Django]

python - Django REST - 使用序列化器创建带有外键的对象

django - 调试 Django 权限错字

c# - 在 MVC Controller 中使用多个参数时,Ajax 表单序列化不绑定(bind)