I'm a relatively new python user, coming from the .NET world.
I'm working on a web application with a python backend (using Flask and SQLAlchemy), where the db models are defined using SQLAlchemy and now I'm writing my first function where I want to retrieve data from the database and bring to the frontend.
Here is a non-functional example (since SQLAlchemy cannot be jsonified directly) just to show what I want to do:
@app.route('/validationrules', methods=["GET"])
@login_required
def getValidationRules():
return jsonify(db.session.query(ValidationRules).all())
My question is, is it customary to create view models when using the stack I mentioned above, and use those to create a bridge between the SQLAlchemy and json? Or do I just create a custom dict in every method and jsonify that?
Some search results also suggested that I create a base model with a to_dict method, and have all my database models inherit from that, but that seems a bit too coupled to me.
What are the best practices for a case like this?