我正在使用Flask-Admin开发一个网络应用程序。
使用ModelViews时,一切都很顺畅,但是当涉及到BaseView的子类化时,我根本无法使它工作。
我正在尝试添加自己的视图,以便显示数据库中的不同数据。
使用BaseView时,我也无法调用数据库。
这是我的代码:
应用/ __初始化__。PY
#!/flask/bin/python
from flask import Flask
app = Flask(__name__)
app.config.from_object('config')
应用/ models.py
#!/flask/bin/python
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey, BOOLEAN
from sqlalchemy.orm import relationship
import random
import string
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
name = Column(String(32))
token = Column(String(16), primary_key=True)
originator = Column(String(12))
active = Column(BOOLEAN, default=True)
logging = Column(BOOLEAN)
def __init__(self, name='', token='no_token', account='test', originator='Test', active=True, logging=0):
self.name = name
self.token = token
self.originator = originator
self.active = active
self.logging = logging
def __str__(self):
return self.name
应用/ views.py
#imports ommited
from app.models import User
#this works totally fine
class UserView(ModelView):
form_columns = ('name', 'originator','account_type')
column_display_pk=True
column_searchable_list = ('name')
form_excluded_columns = ('logging')
def __init__(self, session, **kwargs):
super(UserView, self).__init__(models.User, session, **kwargs)
class CRUDSettings(BaseView):
@expose('/')
def index(self):
#HERE I don't have access to my User DB object.
I tried with db.session.query(User).all() - db is undefined
I tried many other ways but I can't get all users so I can populate the select dropdown box.
users = User <----------------------------- ?
return self.render('crud.html', accounts = users)
模板/ crud.html
{% extends 'admin/master.html' %}
{% block body %}
<div class="row">
<form action="/" id="form_acc" name="form_acc" method="POST">
<label id="lbl_acc">Account:</label>
<select name="d_account" id="d_account">
{% for acc in accounts %}
<option value="{{acc}}" {{'selected' if selected_account==acc else ''}}>{{acc}}</option>
{% endfor %}
</select>
<button class="button " id="brand_submit" type="submit">Go</button>
</form>
</div>
{% endblock %}
run.py
#!/flask/bin/python
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.admin import Admin
from app import views
app = Flask(__name__)
SERVER = ''
PORT = ''
DATABASE = ''
USERNAME = ''
PASSWORD = ''
SSL = ''
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://%s:%s@%s:%s/%s?%s' % (
USERNAME, PASSWORD, SERVER, PORT, DATABASE, SSL)
db = SQLAlchemy(app)
if __name__ == '__main__':
admin = Admin(app)
admin.add_view(views.UserView(db.session))
admin.add_view(views.CRUDSettings(name='CRUD Settings', endpoint='crud'))
app.config['SESSION_TYPE'] = 'filesystem'
app.run('127.0.0.1', 5001, debug=True)
答案 0 :(得分:1)
在调用db
构造函数时传递app CRUDSettings
变量。将您的视图模型重构为以下内容:
class CRUDSettings(BaseView):
def __init__(self, session, name=None, category=None, endpoint=None, url=None, static_folder=None,
menu_class_name=None, menu_icon_type=None, menu_icon_value=None):
self.session = session
super(CRUDSettings, self).__init__(name, category, endpoint, url, static_folder,
menu_class_name=menu_class_name, menu_icon_type=menu_icon_type,
menu_icon_value=menu_icon_value)
@expose('/')
def index(self):
# users = self.session.query(User).all()
现在按如下方式设置您的视图:
admin.add_view(views.CRUDSettings(session=db.session, name='CRUD Settings', endpoint='crud'))