flask_admin的modelview源码解析

Flask_Admin的ModelView是用于数据管理的基础类,它假设提供的模型包含属性且有唯一标识符。为了支持新的数据库,你需要从BaseModelView派生,实现数据相关方法,并自定义表单生成。关键配置包括can_create, can_edit和can_delete,以及各种列表、编辑和创建模板的定制。此外,可以设置显示、排除的字段,以及字段的别名、描述、格式化和排序选项。" 130474111,634897,C++内存越界详解:后果与排查,"['C++', '内存管理', '程序异常', '内存分析工具', '软件调试']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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类

该视图不关心你的模型是如何存储和管理的,但是期望以下的信息

  1. The provided model is an object     提供的模型是个对象
  2. The model contains properties    该模型具有属性
  3. Each model contains an attribute which uniquely identifies it (i.e. a primary key for a database model)    每个模型都至少有一个独一无二的标识符即数据库中的主键
  4. It is possible to retrieve a list of sorted models with pagination applied from a data source   可以从数据源中检索分页模型的列表 
  5. You can get one model by its identifier from the data source    你可以从数据源中通过标识符获取模型

其实如果需要支持一个新的数据库,你只要做以下几件事情

  1. Derive from the BaseModelView class    从BaseModelView派生
  2. Implement various data-related methods (get_list, get_one, create_model, etc)    实现各种数据库相关的方法
  3. 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防御

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值