【问题标题】:get all fields in UpdateView django获取UpdateView django中的所有字段
【发布时间】:2018-11-16 03:49:54
【问题描述】:

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

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”...等),我可以使用一些东西从给定模型中获取所有字段。

【问题讨论】:

  • 您到底想达到什么目标?请发布您现有的代码。
  • 您可以使用模型表单,该表单将为您提供模型的所有可更新字段。
  • 部分更新可以使用put方法。

标签: django field


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-25
    • 1970-01-01
    • 2018-12-27
    • 2017-01-24
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    相关资源
    最近更新 更多