是否可以从给定的 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/