Home > Enterprise >  Getting back property of object in a query list SQLAlchemy
Getting back property of object in a query list SQLAlchemy

Time:12-25

I have a join table called UserServices. Which has a FK of service_id and makes a back ref to a service. Below I get all the userServices in which the id in the route param matches the user_id (another FK) I am then trying to access all the service properties on the all_user_services list.

My current code only returns one dict instead of a list of dicts. What am i doing wrong?

@bp.route('/user/<id>/services', methods=['GET'])
def get_services_from_user(id):
    all_user_services = db_session.query(UserService).filter(UserService.user_id == id).all()

    for service in all_user_services:
        result = service_schema.dump(service.service)
        return jsonify(result)

CodePudding user response:

You just return on first for iteration. You need to create result list:

dumped = [service_schema.dump(s.service) for s in all_user_services]
return jsonify(dumped)
  • Related