在Flask-Restless中遇到数据库连接

时间:2016-05-06 13:51:23

标签: flask-sqlalchemy pythonanywhere flask-restless

我正在尝试使用Flask在PythonAnywhere上提供数据,并希望使用Flask-Restful,但我意识到我有点困惑。

@app.route("/test")
def create_api():
    engine = create_engine(SQLALCHEMY_DATABASE_URI)
    Session = sessionmaker(bind=engine)
    session = Session()



    try:
        db = dataset.connect(SQLALCHEMY_DATABASE_URI)
    except:
        print "couldn't connect to database"


    class Prices(db.Model):
        __tablename__ = 'data'
        pcode_district = db.Column(db.Integer, primary_key=True)
        year_95 = db.Column(db.Float)
        year_15 = db.Column(db.Float)
        percent_change = db.Column(db.Float)
    #Create the Flask-Restless API manager
    manager = flask.ext.restless.APIManager(app,
                                    flask_alchemy_db = db)
    manager.create_api(Prices, methods=['GET'], max_results_per_page =1000)

我得到的错误与定义数据库模型的类有关:

AttributeError: 'Database' object has no attribute 'Model'

我对会话和数据库实例之间的关系感到困惑。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您已使用本地db变量覆盖了db模块。将您分配到的db重命名为db = dataset.connect(SQLALCHEMY_DATABASE_URI)x <- c(1,2,3,4) y <- c(4,1,3,2) z <- c(1,2,3,4) dat <- data.frame(x,y,z) windows(width = 5, height = 9) #quartz() on Mac layout(matrix(c(1,2), 2, 1, byrow = TRUE), heights=c(0.5,1)) par(oma = c(4,3,0,0) + 0.1, mar = c(0,0,1,1) + 0.1) plot(dat$x, y=rep(1,4), type = "n", axes = F, ylab = "", xlab = "") legend(x = "bottomright", legend = c("y", "z"), fill = c("blue", "red")) plot(dat$x, dat$y, type = "n", main = "PLOT") lines(z, col = "red") lines(y, col = "blue")