django - 获取 UpdateView django 中的所有字段

标签 django field

是否可以从给定的 model 中获取所有字段?不写 view.py 中所有字段的列表使用 UpdateView 时的文件在基于类的 View 中?

class UserProfileEditView(UpdateView):
    model = UserProfile
    fields = ['first_name', 'last_name', 'email', 'level', 'course', 'country', 'title', 'avatar', 'icon', 'instagram']
    slug_field = 'username'
    template_name = 'profile.html'
    context_object_name = 'User'

我的意思是不是将所有字段都写在列表中(例如'first_name'、'last_name'...等),我可以使用某些东西从给定模型中获取所有字段。

最佳答案

fields通用基于类的 View 上的属性的工作方式与 ModelForm 上的属性相同。 .您可以自己明确列出这些字段,也可以使用 __all__ ,这表示应使用模型中的所有字段。

class UserProfileEditView(UpdateView):
    model = UserProfile
    fields = '__all__'
    ...

关于django - 获取 UpdateView django 中的所有字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53331153/

相关文章:

python - Django:没有默认值的不可空字段

java - 从 ArrayList 访问特定对象字段

c# - 如何将额外的属性字段添加到 asp.net 下拉列表中

performance - 检索 Lucene 搜索返回的所有文档的某些字段的最佳方法

javascript - 为什么 js 脚本在 django 网站上不起作用?

python - Django 2.2 - 测试用例 - client.logout() 方法不注销用户

python 变量引用意外改变行为

python - Redis 可以写出到像 PostgreSQL 这样的数据库吗?

oracle - 在Oracle中字段值更改时增加行号

C# 反射 : Finding Attributes on a Member Field