-
Notifications
You must be signed in to change notification settings - Fork 0
Description
Security
要在视图上阻止或设置允许的权限,只需使用基本权限设置base_permissions属性即可
class GroupModelView(ModelView):
datamodel = SQLAInterface(Group)
base_permissions = ['can_add','can_delete']有了这个初始配置,框架将只允许在GroupModelView上创建'can_add'和'can_delete'权限。 因此,用户甚至应用程序的管理员将不可能在Group table视图中添加列表或显示权限。 基本可用权限有:can_add,can_edit,can_delete,can_list,can_show。
Custom Fields
322/5000
自定义模型属性可以用在列表上。 这对格式化值(如货币,时间或日期)很有用。 或为自定义HTML。 这非常简单,首先在模型上定义自定义属性,然后使用@renders修饰器告诉框架将类方法映射到某个Model属性:
from flask_appbuilder.models.decorators import renders
class MyModel(Model):
id = Column(Integer, primary_key=True)
name = Column(String(50), unique = True, nullable=False)
custom = Column(Integer(20))
@renders('custom')
def my_custom(self):
# will render this columns as bold on ListWidget
return Markup('<b>' + custom + '</b>')在您的视图中,将您的方法作为列在列表上:
class MyModelView(ModelView):
datamodel = SQLAInterface(MyTable)
list_columns = ['name', 'my_custom']Base Filtering
要过滤视图数据,只需使用基本过滤器设置base_filter属性即可。 这些都将首先应用于任何搜索。 这是非常灵活的,你可以应用多个过滤器的静态值,或基于您定义的函数值。 在下一个示例中,我们通过登录用户过滤视图,并以“a”开头的列名。base_filters是包含3个值的列表[[''列名',FilterClass',过滤值],...]
from flask import g
from flask_appbuilder import ModelView
from flask_appbuilder.models.sqla.interface import SQLAInterface
from flask_appbuilder.models.sqla.filters import FilterStartsWith, FilterEqualFunction
# If you're using Mongo Engine you should import filters like this, everything else is exactly the same
# from flask_appbuilder.models.mongoengine.filters import FilterStartsWith, FilterEqualFunction
from .models import MyTable
def get_user():
return g.user
class MyView(ModelView):
datamodel = SQLAInterface(MyTable)
base_filters = [['created_by', FilterEqualFunction, get_user],
['name', FilterStartsWith, 'a']]Default Order
默认排序 Use a default order on your lists, this can be overridden by the user on the UI. Data structure (‘col_name’:’asc|desc’)
class MyView(ModelView):
datamodel = SQLAInterface(MyTable)
base_order = ('my_col_to_be_ordered','asc')Template Extra Arguments
You can pass extra Jinja2 arguments to your custom template, using extra_args property:
传入额外的参数
class MyView(ModelView):
datamodel = SQLAInterface(MyTable)
extra_args = {'my_extra_arg':'SOMEVALUE'}
show_template = 'my_show_template.html'Forms - Override automatic form creation
使用WTForms定义您自己的添加,编辑表单以覆盖自动创建表单:
class MyView(ModelView):
datamodel = SQLAInterface(MyModel)
add_form = AddFormWTFForms - Add or remove fields
定义哪些列将包含在添加或编辑表单中,例如,如果您拥有像用户或日期这样的自动字段,您可以将它们从Add表单中删除:
class MyView(ModelView):
datamodel = SQLAInterface(MyModel)
add_columns = ['my_field1','my_field2']
edit_columns = ['my_field1']To contribute with any additional fields that are not on a table/model, for example a confirmation field:
用任何不在表格/模型上的附加字段进行贡献,例如确认字段:
class ContactModelView(ModelView):
datamodel = SQLAInterface(Contact)
add_form_extra_fields = {'extra': TextField(gettext('Extra Field'),
description=gettext('Extra Field description'),
widget=BS3TextFieldWidget())}Forms - Readonly fields
Define/override readonly fields like this, first define a new Readonly field:
from flask_appbuilder.fieldwidgets import BS3TextFieldWidget
class BS3TextFieldROWidget(BS3TextFieldWidget):
def __call__(self, field, **kwargs):
kwargs['readonly'] = 'true'
return super(BS3TextFieldROWidget, self).__call__(field, **kwargs)Next override your field using your new widget:
class ExampleView(ModelView):
datamodel = SQLAInterface(ExampleModel)
edit_form_extra_fields = {'field2': TextField('field2',
widget=BS3TextFieldROWidget())}只读选择字段是一个特殊的情况,但它以更简单的方式解决:
# Define the field query
def department_query():
return db.session.query(Department)
class EmployeeView(ModelView):
datamodel = SQLAInterface(Employee)
list_columns = ['employee_number', 'full_name', 'department']
# override the 'department' field, to make it readonly on edit form
edit_form_extra_fields = {'department': QuerySelectField('Department',
query_factory=department_query,
widget=Select2Widget(extra_classes="readonly"))}Forms - Custom validation rules
使用您自己的额外表单验证规则作出贡献。记住,FAB将自动验证数据库中定义的任何字段,而非Null(Required)或唯一约束:
class MyView(ModelView):
datamodel = SQLAInterface(MyModel)
validators_columns = {'my_field1':[EqualTo('my_field2',
message=gettext('fields must match'))
]
}Forms - Custom query on related fields
在相关字段上自定义查询
from flask_appbuilder.models.sqla.filters import FilterStartsWith
class ContactModelView(ModelView):
datamodel = SQLAInterface(Contact)
add_form_query_rel_fields = {'group': [['name',FilterStartsWith,'W']]}这将过滤与ContactGroup模型相关的Contact模型的列表组合。 将以W开头的条目对组合进行过滤。您可以分别使用add_form_quey_rel_fields,edit_form_query_rel_fields,search_form_query_rel_fields定义添加,编辑和搜索的单个过滤器。 看一下API参考如果你想过滤多个相关的字段只需要添加新的键到字典中,记住你也可以为每个字段添加多个过滤器,看看base_filter属性:
class ContactModelView(ModelView):
datamodel = SQLAInterface(Contact)
add_form_query_rel_fields = {'group': [['name',FilterStartsWith,'W']],
'gender': [['name',FilterStartsWith,'M']]}Forms - Related fields
To use AJAX select2 (combo) fields and make use of the REST API, by default all fields are previously populated on the server. Here’s a simple example:
ajax 列子
class ContactModelView(ModelView):
datamodel = SQLAInterface(Contact)
add_form_extra_fields = {
'contact_group': AJAXSelectField('contact_group',
description='This will be populated with AJAX',
datamodel=datamodel,
col_name='contact_group',
widget=Select2AJAXWidget(endpoint='/contactmodelview/api/column/add/contact_group')),
}如果你有两个(或更多)与自己相关的关系,就像联系人上的一个组和一个子组,当用户选择该组,第二个select2组合将会 填充属于该组的子组值。 扩展前面的例子:
class ContactModelView(ModelView):
datamodel = SQLAInterface(Contact)
add_form_extra_fields = {
'contact_group': AJAXSelectField('contact_group',
description='This will be populated with AJAX',
datamodel=datamodel,
col_name='contact_group',
widget=Select2AJAXWidget(endpoint='/contactmodelview/api/column/add/contact_group')),
'contact_sub_group': AJAXSelectField('Extra Field2',
description='Extra Field description',
datamodel=datamodel,
col_name='contact_sub_group',
widget=Select2SlaveAJAXWidget(master_id='contact_group',
endpoint='/contactmodelview/api/column/add/contact_sub_group?_flt_0_contact_group_id={{ID}}'))
}So as seen before add_form_extra_fields is a dictionary that expects keys as column names and values as WTF Fields. AJAXSelectField is expecting the following parameters for the constructor: - label: A label for the column. - description: A description to render on the form. - datamodel: SQLAlchemy initialized with the model. - col_name: The column name. - widget: Use Select2AJAXWidget (for the master) and Select2SlaveAJAXWidget for the slave. - endpoint: The REST API that will be used to populate the select2. You have 3 endpoint’s API that will return data ready to use by this fields: //api/column/add|edit/ : you can append query string’s to filter data. This will return all values of the related column on the model. //api/readvalues: This will return all values on the modelview prepared to be used on a select2.
所以正如之前看到的add_form_extra_fields是一个字典,期望键为列名和值作为WTF字段。 AJAXSelectField期望构造函数的以下参数: - label:列的标签。 - 描述:在表单上呈现的描述。 - datamodel:用模型初始化SQLAlchemy。 - col_name:列名称。 - 小部件:使用Select2AJAXWidget(用于主)和Select2SlaveAJAXWidget作为从机。 - endpoint:将用于填充select2的REST API。 你有3个端点的API将返回准备用这个字段的数据:/ <你的MODELVIEW名称> / api / column / add | edit / :你可以附加查询字符串来过滤数据。 这将返回模型上相关列的所有值。 / <您的MODELVIEW名称> / api / readvalues:这将返回准备在select2上使用的模型视图上的所有值。