[Python] How to add Flask sqlalchemy multiple tables query result with column name & value...

Discussão em 'Python' iniciado por Stack, Fevereiro 8, 2025.

  1. Stack

    Stack Membro Participativo

    I have got 2 tables, one is department and another one is supplier. For every department, there could be a supplier or it could be none. So I use the outerjoin and the result is correct. I need to know how add these results in to a list

    @department.route("/department/getDepartmentDetails/<int:id>", methods=['GET', 'POST'])
    def getDepartmentDetails(id):
    department = db.session.query(Department, Supplier) \
    .outerjoin(Department, Department.default_supplier_id == Supplier.id) \
    .filter(Department.id == id)


    If I query single table like below, I can use getattr to get the desired result

    department = Department.query.filter(Department.id==id).first()
    dept={c.name: str(getattr(department, c.name)) for c in department.__table__.columns}
    return jsonify(dept)


    How do I get the same for the multi-table query result?

    Continue reading...

Compartilhe esta Página