flask_admin model
官方文檔
https://flask-admin.readthedocs.io/en/latest/_modules/flask_admin/model/base/#BaseModelView.get_filter_arg學(xué)習(xí)
文檔地址:http://www.minzhulou.com/docs/flask-admin/api/mod_model.html
model在flask_admin算是比較重要的部分,根據(jù)文檔稍微的解釋下常用的參數(shù)(個(gè)人見(jiàn)解)
這是最基礎(chǔ)的modelview類
該視圖不關(guān)心你的模型是如何存儲(chǔ)和管理的,但是期望以下的信息
The provided model is an object 提供的模型是個(gè)對(duì)象
The model contains properties 該模型具有屬性
Each model contains an attribute which uniquely identifies it (i.e. a primary key for a database model) 每個(gè)模型都至少有一個(gè)獨(dú)一無(wú)二的標(biāo)識(shí)符即數(shù)據(jù)庫(kù)中的主鍵
It is possible to retrieve a list of sorted models with pagination applied from a data source 可以從數(shù)據(jù)源中檢索分頁(yè)模型的列表
You can get one model by its identifier from the data source 你可以從數(shù)據(jù)源中通過(guò)標(biāo)識(shí)符獲取模型
其實(shí)如果需要支持一個(gè)新的數(shù)據(jù)庫(kù),你只要做以下幾件事情
Derive from the BaseModelView class 從BaseModelView派生
Implement various data-related methods 實(shí)現(xiàn)各種數(shù)據(jù)庫(kù)相關(guān)的方法
Implement automatic form generation from the model representation從模型表示中實(shí)現(xiàn)自動(dòng)表單生成
can_create = True 是否可以創(chuàng)建
can_edit = True 是否可以編輯
can_delete = True 是否可以刪除
list_template = 'admin/model/list.html' 修改顯示該模型的html模板
edit_template = 'admin/model/edit.html' 修改編輯該模型的html模板
create_template = 'admin/model/create.html' 修改創(chuàng)建該模型的html模板
column_list 填入想要顯示的字段,不填的話自動(dòng)從模型中取
column_exclude_list 填入不想顯示的字段
column_labels 一個(gè)字典,值是字段名,鍵是顯示的名稱,為字段提供顯示的別名
column_descriptions 一個(gè)字典,同上,為字段顯示描述
column_formatters 一個(gè)字典,格式化字段,定義字段的顯示方式
column_type_formatters 一個(gè)字典,格式化字段類型,定義字段類型的顯示方式,默認(rèn)顯示,None是空字符,bool是True,list是‘,’
column_display_pk 控制主鍵是否顯示
column_sortable_list 選擇可以被排序的字段
column_searchable_list 選擇可以被搜索的字段
column_default_sort 默認(rèn)的排序字段,默認(rèn)為空
column_choices 字段的可選值
column_filters 選擇可以被過(guò)濾的字段
form 一個(gè)Form類,可以被重寫(xiě), 用來(lái)在創(chuàng)建和編輯是使用的表單
form_base_class 一般用來(lái)做csrf防御
form_columns=None 選擇創(chuàng)建或者編輯時(shí)顯示的字段
form_excluded_columns 選擇創(chuàng)建或編輯時(shí)不想顯示的字段
form_args=None 表單字段參數(shù)的字典。有關(guān)可能的選項(xiàng)列表,請(qǐng)參閱WTForms文檔
form_overrides 重寫(xiě)字段的表單字典
form_widget_args 定義表單字典需要的參數(shù)
form_extra_fields 表單額外的字段
form_ajax_refs 使用ajax來(lái)加載外鍵
form_create_rules=None 為創(chuàng)建的表單定制規(guī)則,重寫(xiě)form_rules如果存在的話
form_edit_rules=None 為編輯的表單定制規(guī)則,重寫(xiě)form_rules如果存在的話
action_disallow_list 選擇不允許執(zhí)行的操作,如刪除操作
page_size=20 設(shè)置每頁(yè)顯示的字段數(shù)
action_form 自定義表單操作
action_view 自定義顯示操作
after_model_change 在表單改變之后需要做的事情
after_model_delete 在表單被刪除之后需要做的事情
ajax_update 在列表視圖中編輯單個(gè)字段
can_export=False 是否可以被導(dǎo)出
can_set_page_size=False 是否可以設(shè)置分頁(yè)的數(shù)量
can_view_details=False 是否可以查看詳細(xì)的字段
column_details_exclude_list=None 詳細(xì)字段中不顯示的字段
column_details_list=None 詳細(xì)字段中顯示的字段
column_display_actions=None 控制字段每個(gè)值的操作,編輯,刪除,查看詳細(xì)字段等
column_editable_list=None 可以被編輯的字段
column_export_exclude_list=None 不可以被導(dǎo)出的字段
column_export_list=None 可以被導(dǎo)出的字段
column_extra_row_actions=None 定制額外的字段操作
column_formatters_export=None 定義導(dǎo)出的字段格式
column_type_formatters_export=None 定義導(dǎo)出的字段類型的格式
create_form 創(chuàng)建表單
create_modal=False 創(chuàng)建時(shí)是否彈出對(duì)話框
create_modal_template='/admin/model/modals/create.html' 設(shè)置創(chuàng)建時(shí)彈出的對(duì)話框的模板地址
create_view 創(chuàng)建視圖
delete_form 刪除表單
delete_model 刪除模板
delete_view 刪除視圖
details_modal=Flase 查看詳細(xì)時(shí)是否彈出對(duì)話框
details_modal_template=‘a(chǎn)dmin/model/modals/details.html’ 設(shè)置查看詳細(xì)彈出的對(duì)話框的模板地址
details_template='admin/model/details.html' 設(shè)置查看詳細(xì)的模板地址
details_view 詳細(xì)視圖
edit_form 編輯表單
edit_modal=False 編輯時(shí)是否彈出對(duì)話框
edit_modal_template='admin/model/modals/edit.html' 設(shè)置編輯時(shí)彈出對(duì)話框的模板地址
edit_view 編輯視圖
export_max_row=0 設(shè)置導(dǎo)出最大的數(shù)量
export_types=['csv'] 設(shè)置導(dǎo)出類型
form_rules=None 表單規(guī)則
get_action_form() 為模型操作創(chuàng)建表單類
get_column_name(field) 返回一個(gè)人類可以讀的字段名
get_column_names(only_column, excluded_columns) 返回一系列可以讀的字段名
get_create_form() 為模型創(chuàng)建視圖創(chuàng)建一個(gè)表單類
get_delete_form() 為模型刪除視圖創(chuàng)建一個(gè)表單類
get_details_columns() 獲取詳細(xì)字段的字段名
get_edit_form() 為模型編輯視圖創(chuàng)建一個(gè)表單類
get_export_columns() 獲取可以被導(dǎo)出的字段名
get_export_name(export_type='csv') 獲取可以導(dǎo)出文件名稱
get_export_value(model, name) 獲取導(dǎo)出值
get_filter_arg(index,flt) 獲取單個(gè)過(guò)濾項(xiàng)
get_filters() 獲取所有過(guò)濾項(xiàng)
get_form() 獲取表單類
get_list(page, sort_field, sort_desc, search, filters,page_size=None) 從數(shù)據(jù)庫(kù)中獲取指定的數(shù)據(jù)
get_list_columns() 獲取設(shè)置的column_list中的字段
get_list_form() 獲取可編輯列表視圖的表單類
get_list_row_actions() 返回字段可以執(zhí)行的操作
get_list_value(context,model,name) 返回要在列表視圖中顯示的值
get_one(id) 通過(guò)id來(lái)獲取某個(gè)模型
get_pk_value(model) 獲取模型的主鍵
get_save_return_url(model, is_created=False) 獲取保存之后返回的url
get_sortable_columns() 獲取可以排序的字段
handle_filter(filter) 處理過(guò)濾器
index_view(args,*kwargs) 默認(rèn)顯示的視圖
init_search() 初始化搜索
is_action_allowed(name) 判斷操作是否允許
is_editable(name) 判斷是否可以編輯
is_sortable(name) 判斷是否可以拍下
is_valid_filter(filter) 判斷是否是合法的過(guò)濾器
list_form(obj=None) 實(shí)例化列表視圖的模型編輯表單并返回
named_filter_urls=False 在url參數(shù)中使用人類可以的過(guò)濾器
on_form_prefill(form,id) 執(zhí)行其他操作以預(yù)填充編輯表單
on_model_change(form,model,is_created) 在模板改變后需要做的事情
on_model_delete(model) 在模板被刪除之后需要做的事情
scaffold_filters(name) 為給定的名稱生成過(guò)濾器對(duì)象
scaffold_form() 從模型中創(chuàng)建form.BaseForm繼承的類。必須在子類中實(shí)現(xiàn)
scaffold_list_columns() 返回模型字段名稱列表。必須在子類中實(shí)現(xiàn)。
scaffold_list_form(widget=None, validators=None) 僅使用self.column_editable_list中的列為index_view創(chuàng)建表單
scaffold_sortable_columns() 返回可排序列的字典。必須在子類中實(shí)現(xiàn)
simple_list_pager=False 是否計(jì)數(shù)
update_model(form,model) 從表單中更新模型
validate_form(form) 驗(yàn)證提交的表單