class BaseModelView(model, name=None, category=None, endpoint=None, url=None, static_folder=None, menu_class_name=None, menu_icon_type=None, menu_icon_value=None)
这是最基础的modelview类
该视图不关心你的模型是如何存储和管理的,但是期望以下的信息
- The provided model is an object 提供的模型是个对象
- The model contains properties 该模型具有属性
- Each model contains an attribute which uniquely identifies it (i.e. a primary key for a database model) 每个模型都至少有一个独一无二的标识符即数据库中的主键
- It is possible to retrieve a list of sorted models with pagination applied from a data source 可以从数据源中检索分页模型的列表
- You can get one model by its identifier from the data source 你可以从数据源中通过标识符获取模型
其实如果需要支持一个新的数据库,你只要做以下几件事情
- Derive from the BaseModelView class 从BaseModelView派生
- Implement various data-related methods (get_list, get_one, create_model, etc) 实现各种数据库相关的方法
- Implement automatic form generation from the model representation (scaffold_form) 从模型表示中实现自动表单生成
can_create = True 是否可以创建
can_edit = True 是否可以编辑
注:can_create, can_edit 为True时,同时需要设置form_columns或者form_excluded_columns属性,个人没有设置会报错:
File "/usr/local/lib/python2.7/dist-packages/wtforms/widgets/core.py", line 301, in render_option
return HTMLString('<option %s>%s</option>' % (html_params(**options), escape(text_type(label), quote=False)))
TypeError: coercing to Unicode: need string or buffer, long found
can_delete = True 是否可以删除
list_template = ‘admin/model/list.html‘ 修改显示该模型的html模板
edit_template = ‘admin/model/edit.html‘ 修改编辑该模型的html模板
create_template = ‘admin/model/create.html‘ 修改创建该模型的html模板
column_list 填入想要显示的字段,不填的话自动从模型中取
column_exclude_list 填入不想显示的字段
column_labels 一个字典,值是字段名,键是显示的名称,为字段提供显示的别名
column_descriptions 一个字典,同上,为字段显示描述
column_formatters 一个字典,格式化字段,定义字段的显示方式
column_type_formatters 一个字典,格式化字段类型,定义字段类型的显示方式,默认显示,None是空字符,bool是True,list是‘,’
column_display_pk 控制主键是否显示
column_sortable_list 选择可以被排序的字段,默认全部可以被排序
column_searchable_list 选择可以被搜索的字段
column_default_sort 默认的排序字段,默认为空
column_choices 字段的可选值
column_filters 选择可以被过滤的字段
form 一个Form类,可以被重写, 用来在创建和编辑时使用的表单
form_base_class 一般用来做csrf防御