我認爲你的第二個選項可能有一個可能的實現。例如,在Flask-Admin中,你可以製作
from flask import Flask
from flask.ext.admin import Admin, BaseView, expose
class MyView(BaseView):
@expose('/')
def index(self):
return self.render('index.html')
app = Flask(__name__)
admin = Admin(app)
admin.add_view(MyView(name='Hello 1', endpoint='test1', category='Test'))
admin.add_view(MyView(name='Hello 2', endpoint='test2', category='Test'))
admin.add_view(MyView(name='Hello 3', endpoint='test3', category='Test'))
app.run()
它給你一個帶有多個視圖的下拉菜單。你可以將MyView
作爲ModelView
實例化,並動態遍歷表。即
from flask import Flask
from flask.ext.admin import Admin, BaseView, expose
class MyView(ModelView):
# Disable model creation
can_create = False
# Override displayed fields
column_list = ('field1', 'field2')
def __init__(self, session, **kwargs):
# If tables model is the same, otherwise, you need
# to generate ``TableModel`` dynamically
super(MyView, self).__init__(TableModel, session, **kwargs)
app = Flask(__name__)
admin = Admin(app)
for index, name, endpoint in get_dynamic_table_datalist():
admin.add_view(MyView(session=db.session, name=name,
endpoint=endpoint, category='Tables'))
app.run()
希望這會有所幫助。乾杯..
不幸的是,這不允許用戶在執行'app.run()'後創建/刪除表格。 如果我將'admin.add_view()'方法移動到'app.run()'之後,瓶子將引發一個異常,說明應用程序初始化後無法添加視圖。 – Rafael