简体   繁体   English

如何在模型类的Flask Admin视图中使字段不可编辑

[英]How to make a field non-editable in Flask Admin view of a model class

I have a User model class and password is one attribute among many. 我有一个User模型类, password是许多中的一个属性。 I am using Flask web framework and Flask-Admin extension to create the admin view of my model classes. 我正在使用Flask Web框架和Flask-Admin扩展来创建我的模型类的管理视图。 I want to make certain fields in the admin view like password non editable or not show them at all. 我想在管理视图中创建某些字段,例如password不可编辑或根本不显示它们。 How do I do it? 我该怎么做?

I can make the fields not show up in the normal view but when I click on the edit button of any record in the table, all fields show up and are editable. 我可以使字段不显示在普通视图中,但是当我单击表格中任何记录的编辑按钮时,所有字段都会显示并且可以编辑。

You should extend your view from ModelView and overwrite the necessary fields. 您应该从ModelView扩展视图并覆盖必要的字段。

In my class it looks like this: 在我的课堂上,它看起来像这样:

class UserView(ModelView):

    column_list = ('first_name', 'last_name', 'username', 'email')
    searchable_columns = ('username', 'email')
# this is to exclude the password field from list_view:
    excluded_list_columns = ['password']
    can_create = True
    can_delete = False
# If you want to make them not editable in form view: use this piece:
    form_widget_args = {
        'name': {
            'readonly': True
        },
    }

Hope this helps! 希望这可以帮助! For more information check out the documentation: 有关更多信息,请查看文档:

Here is a solution that expands upon Remo's answer and this so answer . 这是一个解决方案,扩展了雷莫的答案和这个答案 It allows for different field_args for edit and create forms. 它允许使用不同的field_args进行编辑和创建表单。

Custom Field Rule Class 自定义字段规则类

from flask_admin.form.rules import Field

class CustomizableField(Field):
    def __init__(self, field_name, render_field='lib.render_field', field_args={}):
        super(CustomizableField, self).__init__(field_name, render_field)
        self.extra_field_args = field_args

    def __call__(self, form, form_opts=None, field_args={}):
        field_args.update(self.extra_field_args)
        return super(CustomizableField, self).__call__(form, form_opts, field_args)

UserView Class UserView类

class UserView(ModelView):

    column_list = ('first_name', 'last_name', 'username', 'email')
    searchable_columns = ('username', 'email')

    # this is to exclude the password field from list_view:
    excluded_list_columns = ['password']
    can_create = True
    can_delete = False

    # If you want to make them not editable in form view: use this piece:
    form_edit_rules = [
        CustomizableField('name', field_args={
            'readonly': True
        }),
        # ... place other rules here
    ]

Yet another way to work the problem around is to use Flask-Admin ModelView method called on_form_prefill to set readonly property argument. 另一种解决问题的方法是使用名为on_form_prefill Flask-Admin on_form_prefill方法来设置readonly属性参数。 According to Flask-Admin Docs : Flask-Admin Docs说

on_form_prefill (form, id) on_form_prefill (form,id)

Perform additional actions to pre-fill the edit form. 执行其他操作以预填充编辑表单。

Called from edit_view, if the current action is rendering the form rather than receiving client side input, after default pre-filling has been performed. 从edit_view调用,如果当前操作正在呈现表单而不是接收客户端输入,则在执行默认预填充之后。

In other words, this is a trigger, which is run when opening only Edit form, not the Create one. 换句话说,这是一个触发器,仅在打开编辑表单而不是创建表单时运行。

So, the solution for the example used above would be: 因此,上面使用的示例的解决方案将是:

class UserView(ModelView):
    ...
    def on_form_prefill(self, form, id):
        form.name.render_kw = {'readonly': True}

The method is run after all other rules applied, so none of them are broken, including set of columns. 该方法在应用所有其他规则后运​​行,因此它们都不会被破坏,包括列集。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 django 管理员中创建“仅创建”不可编辑字段 - How to make a “create-only” non-editable field in django admin 创建一个仅显示(不可编辑)Django 管理字段 - Creating a display-only (non-editable) Django admin field 如何在 Django 中向自定义管理表单添加不可编辑的字段 - How do you add a non-editable field to a custom admin form in Django 无法为文章模型表单指定“内容”,因为它是不可编辑的字段 - 'content' cannot be specified for Article model form as it is a non-editable field Django:如何在内联模型表单集中默认情况下使字段不可编辑? - Django: How do I make fields non-editable by default in an inline model formset? Django管理员,使用DateTime小部件在创建时设置了不可编辑的日期字段 - Django admin, setting a non-editable date field on creation with the DateTime widget Django:无法为订单模型表单指定“已创建”,因为它是不可编辑的字段 - Django: 'created' cannot be specified for Order model form as it is a non-editable field django.core.exceptions.FieldError:不能为论坛 model 表单指定“日期”,因为它是不可编辑的字段 - django.core.exceptions.FieldError: 'date' cannot be specified for Forum model form as it is a non-editable field 如何在 QTableWidget 中使特定单元格可编辑而其余单元格不可编辑? - How to make particular cell editable and leave the rest non-editable in QTableWidget? 如何在flask-admin中的创建视图中排除模型字段? - How to exclude a model field from the create view in flask-admin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM