Python Forum

Full Version: Restful API Python Flask Postman
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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.