Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Restful API Python Flask Postman
#1
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
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python3 - How to deny access to users without api key using localhost and postman Alan2023 0 967 Feb-20-2023, 02:50 PM
Last Post: Alan2023
  Creating a RESTful web service and web service client dangermaus33 0 1,281 Dec-01-2020, 09:56 PM
Last Post: dangermaus33
  python 3.7 on windows using flask and flask-sqlalchemy. Alpy 2 4,013 Aug-12-2020, 07:24 PM
Last Post: Alpy
  RESTful API in Google Cloud Functions fggmack 0 2,011 Dec-30-2018, 06:58 AM
Last Post: fggmack

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020