简体   繁体   English

使用sqlalchemy的python flask_restless不生成api端点并使用蓝图提供“没有属性扩展”错误

[英]python flask_restless with sqlalchemy doesn't generate api endpoints and gives “has no attribute extensions” error using blueprints

for my webapp I want to automatically generate a REST API. 对于我的webapp,我想自动生成REST API。 I read the of flask_restless docs , tried several tutorials and did everything as described: 我阅读了flask_restless 文档 ,尝试了几个教程并完成了所描述的所有内容:

my routes are to be located in module app.main.api.pv_tool.py 我的路线位于模块app.main.api.pv_tool.py中

from . import api
from app.__init__ import db, app
from app.models import University
import flask_restless

manager = flask_restless.APIManager(app, session=db.scoped_session)
manager.create_api(University, methods=['GET'])

@api.route('/')
def index():
    return 'asdf'

where University is defined in models.py 大学在models.py中定义

from sqlalchemy import String, Integer
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

...

class University(Base):
    __tablename__ = 'unis'
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(64), nullable=False, unique=True)

    def __repr__(self):
        return '<Uni %r>' % self.name

...

and the connection to the existing and filled database is established in database.py : 与现有的和充满数据库的连接是建立在database.py:

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session

from app.models import Base


class Database:
    path = 'sqlite:///pv_tool.sqlite'
    engine = None
    session = None
    scoped_session = None

    def __init__(self, path=path):
        self.engine = create_engine(path, convert_unicode=True)
        Base.metadata.bind = self.engine

        db_session = sessionmaker(bind=self.engine, autocommit=False)
        db_session.bind = self.engine
        self.session = db_session()
        self.scoped_session = scoped_session(self.session)
        Base.metadata.create_all()

Now in my browser or using requests library module I get 'asdf' response on http://localhost:5000 . 现在在我的浏览器中或使用requests库模块,我在http:// localhost:5000上得到'asdf'响应。

However, I cannot reach my University data trying everything like: localhost:5000/unis, localhost:5000/api/unis and so on. 但是,我无法访问我的大学数据尝试所有的东西:localhost:5000 / unis,localhost:5000 / api / unis等等。 I always get a 404 response. 我总是收到404回复。

Without any errors this issue is hard to pursue. 没有任何错误,这个问题很难实现。 Does anybody have an idea how I could debug this in general and what my error in the code might be? 有没有人知道我如何调试这一般以及代码中的错误是什么?

In PyCharm Debugger the manager object seems to have some API to create: 在PyCharm Debugger中,manager对象似乎有一些API可以创建: PyCharm But I have no idea what I could look for here next. 但我不知道接下来我能找到什么。

Also, I get an error on runtime when I try using create_api_blueprints in pv_tool.py: 另外,当我尝试在pv_tool.py中使用create_api_blueprints时,我在运行时遇到错误:

...
manager = flask_restless.APIManager(app, session=db.scoped_session)
api_bp = manager.create_api_blueprint('unis', University, methods=['GET'])
app.register_blueprint(api_bp)
...


Traceback (most recent call last):   File "run.py", line 5, in <module>
    from app import app   File "/Users/rich/code/src/github.com/pv-tool-backend/app/__init__.py", line 4, in <module>
    from app.main.api import api   File "/Users/rich/code/src/github.com/pv-tool-backend/app/main/api/__init__.py", line 5, in <module>
    from . import pv_tool   File "/Users/rich/code/src/github.com/pv-tool-backend/app/main/api/pv_tool.py", line 11, in <module>
    api_bp = manager.create_api_blueprint('unis', University, methods=['GET'])   File "/Users/rich/code/src/github.com/pv-tool-backend/pv_tool_backend_venv/lib/python3.6/site-packages/flask_restless/manager.py", line 549, in create_api_blueprint
    restlessinfo = app.extensions['restless'] AttributeError: type object 'University' has no attribute 'extensions'

This is how I run the app: 这是我运行应用程序的方式:

run.py run.py

from app import app
app.run(debug=True, host='0.0.0.0', port=5000)

app module's init.py app模块的init.py

from flask import Flask
from flask_cors import CORS
from app.main.api import api
from app.database import Database


db = Database()

app = Flask(__name__)
app.register_blueprint(api)

CORS(app)

Starting in terminal: 从终端开始:

rich@local:~ $ python run.py
  • Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) http://0.0.0.0:5000/上运行(按CTRL + C退出)
  • Restarting with stat 使用stat重新启动
  • Debugger is active! 调试器处于活动状态!
  • Debugger PIN: 122-170-707 调试器PIN:122-170-707

我设法通过转换使用flask_sqlalchemy而不是纯sqlalchemy来使其工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM