flask-admin 如何在 viewmodel 中访问模型
flask-admin how to access model in viewmodel
我想像这样访问后代 ModelView 中的模型:
class MyAppLibraryView(MyModelView):
if model != Salary:
column_searchable_list = ['name', ]
form_excluded_columns = ['date_created', 'date_modified', ]
column_display_pk = True
这个class的用法如下:
admin.add_view(MyAppLibraryView(Section, db.session))
admin.add_view(MyAppLibraryView(Office, db.session))
admin.add_view(MyAppLibraryView(Salary_reference, db.session))
admin.add_view(MyAppLibraryView(Salary, db.session))
在 MyAppLibraryView 中,class 不知道模型。如何访问传递给 class MyAppLibraryView 的模型?
如您在 flask-admin code 中所见,您可以使用 self.model
访问模型。
您有一些方法可以根据模型覆盖 column_searchable_list
。
- 子类(我推荐的方式)
class MyAppLibraryView(MyModelView):
form_excluded_columns = ['date_created', 'date_modified', ]
column_display_pk = True
class MyAppLibrarySalaryView(MyAppLibraryView):
column_searchable_list = ['name', ]
admin.add_view(MyAppLibrarySalaryView(Salary, db.session))
- 可以在
__init__
中设置。
class MyAppLibraryView(MyModelView):
def __init__(self, model, *args, **kwargs):
if model != Salary:
self.column_searchable_list = ['name',]
]
super(MyAppLibraryView, self).__init__(model, *args, **kwargs)
我想像这样访问后代 ModelView 中的模型:
class MyAppLibraryView(MyModelView):
if model != Salary:
column_searchable_list = ['name', ]
form_excluded_columns = ['date_created', 'date_modified', ]
column_display_pk = True
这个class的用法如下:
admin.add_view(MyAppLibraryView(Section, db.session))
admin.add_view(MyAppLibraryView(Office, db.session))
admin.add_view(MyAppLibraryView(Salary_reference, db.session))
admin.add_view(MyAppLibraryView(Salary, db.session))
在 MyAppLibraryView 中,class 不知道模型。如何访问传递给 class MyAppLibraryView 的模型?
如您在 flask-admin code 中所见,您可以使用 self.model
访问模型。
您有一些方法可以根据模型覆盖 column_searchable_list
。
- 子类(我推荐的方式)
class MyAppLibraryView(MyModelView):
form_excluded_columns = ['date_created', 'date_modified', ]
column_display_pk = True
class MyAppLibrarySalaryView(MyAppLibraryView):
column_searchable_list = ['name', ]
admin.add_view(MyAppLibrarySalaryView(Salary, db.session))
- 可以在
__init__
中设置。
class MyAppLibraryView(MyModelView):
def __init__(self, model, *args, **kwargs):
if model != Salary:
self.column_searchable_list = ['name',]
]
super(MyAppLibraryView, self).__init__(model, *args, **kwargs)