有沒有辦法在數據庫視圖上在Flask-Admin上創建管理視圖?我使用的是Postgres,並創建了一個觀點,讓我們下面的一個爲例:在postgres數據庫視圖上的Flask管理
CREATE VIEW test_view AS (
SELECT id, name, age, score
FROM student
);
如果我想爲設置管理的看法,我會怎麼做呢?
我試圖爲它創造一個抽象的模型:
class StudentView(sqa.Model):
__abstract__ = True
__tablename__ = 'test_view'
id = sqa.Column(sqa.Integer)
name = sqa.Column(sqa.String)
age = sqa.Column(sqa.Integer)
score = sqa.Column(sqa.Integer)
而且添加這樣的管理視圖:
from flask_admin.contrib.sqla import ModelView
class StudentViewAdmin(ModelView):
_model = models.StudentView
註冊該管理視圖管理:
admin.add_view(admin_views.StudentViewAdmin(admin_views.DashboardView123._model, db.session))
但是,我遇到以下錯誤,同時將視圖添加到管理員:
Traceback (most recent call last):
File "manage.py", line 8, in <module>
setup_admin(app)
File "/Users/rohitjain/app.py", line 35, in setup_admin
admin.add_view(admin_views.StudentViewAdmin(admin_views.StudentViewAdmin._model, db.session))
File "/Users/rohitjain/.venvs/venv/lib/python3.5/site-packages/flask_admin/contrib/sqla/view.py", line 318, in __init__
menu_icon_value=menu_icon_value)
File "/Users/rohitjain/.venvs/venv/lib/python3.5/site-packages/flask_admin/model/base.py", line 771, in __init__
self._refresh_cache()
File "/Users/rohitjain/.venvs/venv/lib/python3.5/site-packages/flask_admin/model/base.py", line 847, in _refresh_cache
self._list_columns = self.get_list_columns()
File "/Users/rohitjain/.venvs/venv/lib/python3.5/site-packages/flask_admin/model/base.py", line 979, in get_list_columns
only_columns=self.column_list or self.scaffold_list_columns(),
File "/Users/rohitjain/.venvs/venv/lib/python3.5/site-packages/flask_admin/contrib/sqla/view.py", line 404, in scaffold_list_columns
for p in self._get_model_iterator():
File "/Users/rohitjain/.venvs/venv/lib/python3.5/site-packages/flask_admin/contrib/sqla/view.py", line 340, in _get_model_iterator
return model._sa_class_manager.mapper.iterate_properties
AttributeError: type object 'StudentView' has no attribute '_sa_class_manager'
我們可以這樣做嗎?我已經在Django中完成了這項工作,爲postgres數據庫視圖設置了管理員,但直到現在我無法在Flask中設置相同的值。任何線索?
啊!得到它了。剛剛讓我的一個專欄成爲模型本身的主鍵,而且工作起來。我認爲它不需要成爲視圖本身的主鍵。 –
@RohitJain哦,在答案中澄清它,謝謝! –
非常感謝。儀表板啓動並運行。看起來以前的答案也是正確的。 –