我想像这样在后代ModelView中访问模型:
class MyAppLibraryView(MyModelView):
if model != Salary:
column_searchable_list = ['name', ]
form_excluded_columns = ['date_created', 'date_modified', ]
column_display_pk = True
该类的用法如下:
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中,该类未知模型。如何访问传递给类MyAppLibraryView的模型?
答案 0 :(得分:0)
正如您在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)