Python Forum
Restful API Python Flask Postman - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Restful API Python Flask Postman (/thread-21207.html)



Restful API Python Flask Postman - starter_student - Sep-19-2019

already since 1 week I try to build a simple CRUD RESTful API without success. So when I create/(test) user and post in the cmd I can list them (User.query.all()) but when I try to get all users via my function get_all_users() I get this error: TypeError: 'Engine' object is not callable
Here's my user data model.
#modules importieren

# Create Flask app
app = Flask(__name__)

# Flask-SQLAlchemy settings
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db' # File-based SQL database
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False # Avoids SQLAlchemy warning

# Initialize Flask-SQLAlchemy
db = SQLAlchemy(app)

# Initialize Flask-API
api = Api(app)

# Import routes
from webapp import routes
models.py
class User(db.Model, UserMixin):
    
   # __tablename__ = 'user' (habe auch damit versucht aber ändert sich nicht)

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
    password = db.Column(db.String(60), nullable=False)
    posts = db.relationship('Post', backref='author', lazy=True)

    def __repr__(self):
        return f"User('{self.username}', '{self.email}', '{self.image_file}')"
in routes.py
@app.route('/api/users/', methods = ['GET'])
def get_all_user():
    
    engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'])
    
    conn = engine.connect()
    
    #metadata = db.Metadata()
    
    user = db.engine('user', autoload=True, autoload_with=engine, engine_options=None)

    req = db.select([user])
    
    result = conn.execute(req)
    
    row_result = result.query.all()
    
    return jsonify(row_result)

    conn.close()
in Postman with the URL : http://127.0.0.1:5000/api/users/
I get this error message : TypeError: 'Engine' object is not callable // Tool Debugger

Thank you for your help

B. regards
Karl


RE: Restful API Python Flask Postman - ndc85430 - Sep-22-2019

You should really post the complete traceback, as it gives important information about the error (i.e. which line it occurs on). In any case, the problem is self explanatory: you're basically trying to use an Engine object as a function (i.e. trying to call it), but that doesn't make sense. Consult the SQLAlchemy docs to see how to use it correctly.