如何在模型类的Flask管理视图中使字段不可编辑 [英] How to make a field non-editable in Flask Admin view of a model class

查看:1305
本文介绍了如何在模型类的Flask管理视图中使字段不可编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户模型类和密码是其中的一个属性。我正在使用Flask Web框架和Flask-Admin扩展来创建我的模型类的管理视图。我想在管理视图中使某些字段像密码不可编辑或根本不显示。

我可以使字段在普通视图中不显示,但是当我单击表中任何记录的编辑按钮时,所有字段显示和可编辑。

解决方案

您应该从ModelView扩展您的视图,并覆盖必要的字段。
$ b

在我的类中,它看起来像这样:

$ p $ class UserView(ModelView):

column_list =('first_name','last_name','username','email')
searchable_columns =('username','email')
#这是排除密码来自list_view的字段:
excluded_list_columns = ['password']
can_create = True
can_delete = False
#如果你想让它们在表单视图中不可编辑:
form_widget_args = {
'name':{
'readonly':True
},
}

希望这有助于!
有关更多信息,请查阅文档:


I have a User model class and password is one attribute among many. I am using Flask web framework and Flask-Admin extension to create the admin view of my model classes. I want to make certain fields in the admin view like password non editable or not show them at all. 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.

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:

这篇关于如何在模型类的Flask管理视图中使字段不可编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆