gpt4 book ai didi

python - Flask-Security:如何在主 py 文件之外设置数据库?

转载 作者:行者123 更新时间:2023-12-03 17:03:58 24 4
gpt4 key购买 nike

我正在使用 Flask-Security 1.7.4 和 Flask 0.10.1。

如果数据库方案存储在 python 文件 app.py 中,我运行我的网站没有问题:

from flask.ext.security import RoleMixin, UserMixin, SQLAlchemyUserDatastore, Security
from flask.ext.sqlalchemy import SQLAlchemy

app = Flask(__name__)

#Create database connection object
db = SQLAlchemy(app)

#Define models
roles_users = db.Table('roles_users',
db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))

class Role(db.Model, RoleMixin):
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(80), unique=True)
description = db.Column(db.String(255))

class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(255), unique=True)
companyid = db.Column(db.String(255), unique=True)
lastname = db.Column(db.String(255))
firstname = db.Column(db.String(255))
password = db.Column(db.String(255))
active = db.Column(db.Boolean())
confirmed_at = db.Column(db.DateTime())
roles = db.relationship('Role', secondary=roles_users,
backref=db.backref('users', lazy='dynamic'))

# Setup Flask-Security
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore)

但是我想分离更多我的代码,这样我就可以拥有:
app_folder
|__ app.py # Main file for the app
|__ security.py # File that contain the database for the login/roles

所以我尝试了:

security.py :
db = SQLAlchemy()  

# Define models
roles_users = db.Table(
'roles_users',
db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
db.Column('role_id', db.Integer(), db.ForeignKey('role.id'))
)

class Role(db.Model, RoleMixin):
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(80), unique=True)
description = db.Column(db.String(255))

class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(255), unique=True)
companyid = db.Column(db.String(255), unique=True)
lastname = db.Column(db.String(255))
firstname = db.Column(db.String(255))
password = db.Column(db.String(255))
active = db.Column(db.Boolean())
confirmed_at = db.Column(db.DateTime())
roles = db.relationship('Role', secondary=roles_users, backref=db.backref('users', lazy='dynamic'))

user_datastore = SQLAlchemyUserDatastore(db, User, Role)

app.py :
from flask.ext.security import RoleMixin, UserMixin, SQLAlchemyUserDatastore, Security
from flask.ext.sqlalchemy import SQLAlchemy
from security import db, user_datastore

app = Flask(__name__)

#Initiate the database connection object
db.init_app(app)

# Setup Flask-Security
security = Security(app, user_datastore)

我收到以下错误:
File "C:\Work\pythonVirtualEnv\env02\lib\site-packages\sqlalchemy\engine\default.py", line 436, in do_execute
cursor.execute(statement, parameters)
OperationalError: (OperationalError) no such table: users u'SELECT users.user_id AS users_user_id, users.username AS users_username, users.password AS users_password, users.email AS users_email, users.registered_on AS users_registered_on \nFROM users \nWHERE users.password = ? AND users.email = ?\n LIMIT ? OFFSET ?' (u'test', u'demo@demo.com', 1, 0)

问题是,如果我将所有代码保留在 app.py 中它完美无缺。
当然数据库要存在。

你能告诉我我做错了什么吗?

谢谢 :)

最佳答案

您应该使用不同的结构(和建议的结构):

proj_folder
- run.py
- config.py
|__ app # Main folder for the app
|__ __init__.py
|__ models.py
|__ security.py
|__ views.py

在模型上,您保留您的应用模型
在安全方面,您保留您的安全模型
在 View 上,您​​创建 View 并公开您的 url

然后在 初始化 .py
import logging
from flask import Flask
from flask.ext.appbuilder import SQLA
from flask.ext.appbuilder import AppBuilder
from sqlalchemy import SQLAlchemy

logging.basicConfig(format='%(asctime)s:%(levelname)s:%(name)s:%(message)s')
logging.getLogger().setLevel(logging.DEBUG)

app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)

from app import models, security, views

在运行.py
from app import app

app.run(host='0.0.0.0', port=8080, debug=True)

希望这可以帮助

看看 https://github.com/dpgaspar/Flask-AppBuilder-Skeleton

关于python - Flask-Security:如何在主 py 文件之外设置数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26752928/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com