0

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?

elksie5000
  • 7,084
  • 12
  • 57
  • 87
  • Where's the `dataset` variable coming from in your code? – Giles Thomas May 06 '16 at 16:25
  • I was using dataset module to connect to database. – elksie5000 May 12 '16 at 19:23
  • I don't know the `dataset` module, but is it deliberate that you're defining your `Prices` class as a subclass of something based on the database connection you're getting back from it? As Glenn says in his answer to this question, the `db.Model` that you're specifying as a superclass is something contained in the `dataset` connection. If you're intending it to be a class from some other `db` module then you should use a different variable name for the `dataset` connection. – Giles Thomas May 14 '16 at 17:15

1 Answers1

1

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.

Glenn
  • 7,262
  • 1
  • 17
  • 23