0

我一直试图让https://github.com/graup/flask-restless-security上的代码工作一段时间。在我未能将其合并到我自己的代码中之后,我决定只使用这个准备好的代码。但是,当我尝试运行 server.py 时,它会引发以下错误。错误的根源是flask-restless的apimanager。我在 git 上打开了一个问题,但显然该项目不再维护。我怎样才能使这项工作?

Traceback (most recent call last):
File "E:/WEB/Project1/app/server.py", line 62, in 
include_columns=['id', 'data1', 'data2', 'user_id'])
File "E:\WEB\Project1\flask\lib\site-packages\flask_restless\manager.py", line 698, in create_api
blueprint = self.create_api_blueprint(app=app, *args, **kw)
File "E:\WEB\Project1\flask\lib\site-packages\flask_restless\manager.py", line 566, in create_api_blueprint
pk_name = primary_key or primary_key_name(model)
File "E:\WEB\Project1\flask\lib\site-packages\flask_restless\helpers.py", line 232, in primary_key_name
pk_names = primary_key_names(model)
File "E:\WEB\Project1\flask\lib\site-packages\flask_restless\helpers.py", line 214, in primary_key_names
return [key for key, field in inspect.getmembers(model)
File "c:\python27\Lib\inspect.py", line 252, in getmembers
value = getattr(object, key)
File "E:\WEB\Project1\flask\lib\site-packages\flask_sqlalchemy_init_.py", line 498, in get
return type.query_class(mapper, session=self.sa.session())
File "E:\WEB\Project1\flask\lib\site-packages\sqlalchemy\orm\scoping.py", line 78, in call
return self.registry()
File "E:\WEB\Project1\flask\lib\site-packages\sqlalchemy\util_collections.py", line 990, in call
return self.registry.setdefault(key, self.createfunc())
File "E:\WEB\Project1\flask\lib\site-packages\sqlalchemy\orm\session.py", line 2829, in call
return self.class_(**local_kw)
File "E:\WEB\Project1\flask\lib\site-packages\flask_sqlalchemy_init_.py", line 143, in init
self.app = app = db.get_app()
File "E:\WEB\Project1\flask\lib\site-packages\flask_sqlalchemy_init_.py", line 957, in get_app
'application not registered on db instance and no application'
RuntimeError: application not registered on db instance and no applicationbound to current context

server.py

from database import db
from application import app
from flask_restless import APIManager
from models import SomeStuff

apimanager = APIManager(app, flask_sqlalchemy_db=db)
apimanager.create_api(SomeStuff,
    methods=['GET', 'POST', 'DELETE', 'PUT'],
    url_prefix='/api/v1',
    collection_name='free_stuff',
    include_columns=['id', 'data1', 'data2', 'user_id'])

application.py

from flask import Flask

app = Flask(__name__)
app.config.from_object('config.DevelopmentConfig')

database.py

from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()

模型.py

from database import db
from flask_security import UserMixin, RoleMixin, SQLAlchemyUserDatastore

class SomeStuff(db.Model):
    __tablename__ = 'somestuff'
    id = db.Column(db.Integer, primary_key=True)
    data1 = db.Column(db.Integer)
    data2 = db.Column(db.String(10))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=True)
    user = db.relationship(User, lazy='joined', join_depth=1, viewonly=True)
4

1 回答 1

3

您现在遇到的错误:

flask_sqlalchemy_init_.py RuntimeError:应用程序未在数据库实例上注册并且没有应用程序绑定到当前上下文

正在flask_sqlalchemy使用中,没有定义app实例。

db解决方案是在实例上注册应用程序。

from flask_sqlalchemy import SQLAlchemy
from application import app

db = SQLAlchemy(app)
于 2017-03-24T19:10:08.467 回答