获取 UpdateView django 中的所有字段

Sho*_*rov 7 django field

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

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'
Run Code Online (Sandbox Code Playgroud)

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

Wil*_*ing 13

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

class UserProfileEditView(UpdateView):
    model = UserProfile
    fields = '__all__'
    ...
Run Code Online (Sandbox Code Playgroud)