elksie5000
elksie5000

Reputation: 7762

Getting stuck with database connections in Flask-Restless

I'm trying to use Flask to serve up data on PythonAnywhere and looking to use the Flask-Restful, but realise I'm getting a little confused.

@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)

The error I get ties to the class that defines the database model:

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

I'm getting confused between the relationship between the session and the database instances.

What am I doing wrong?

Upvotes: 0

Views: 296

Answers (1)

Glenn
Glenn

Reputation: 5776

You've overwritten the db module with your local db variable. Rename the db that you're assigning to here: db = dataset.connect(SQLALCHEMY_DATABASE_URI) to something else.

Upvotes: 1

Related Questions