获取 UpdateView django 中的所有字段

get all fields in UpdateView django

在 class 中使用 UpdateView 时,是否可以从给定的 model 中获取所有字段而无需在 view.py 文件中写入所有字段的列表基于视图?

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'... 等),我可以使用一些东西来获取给定的所有字段吗型号。

基于 class 的通用视图上的 fields 属性与 ModelForm 上的属性相同。您可以自己明确列出字段,也可以使用 __all__,这表示应使用模型中的所有字段。

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