Flask-SQLAlchemy动态参数查询无结果,硬编码正常的问题求助
我正在为电子健康记录(EHR)系统的下拉菜单填充客户ID与名称列表。在get_client路由中,用户选择客户后,作为外键的client_id会被传递到progress_view路由,该路由查询进度笔记数组并传递给progress_view.html模板。但模板的滚动div为空,无错误信息也无数据。
我尝试过传递命名参数、session['client_id']值和request.args.get('client_id'),为排查问题,我将client_id作为标量和数组一起传递给模板:return render_template('progress_view.html', notes=results, client_id=client_id),无论如何传递,client_id都能在模板顶部显示,说明参数已被解析。之后我尝试了SQLAlchemy的不同语法,结果依然无数据。于是我创建了progress_test路由,将client_id硬编码,此时模板的滚动div能正常填充数据,且client_id也能正常显示。不知该如何继续排查。
路由代码
def get_client(): clients = db.session.query(MPI).filter().all() if request.method == "POST": client_id = request.form.get("client_id") if client_id: session['client_id'] = client_id # return render_template('progress_view.html', client_id=client_id) return redirect(url_for("chart.progress_view")) return render_template('get_client.html', clients=clients) # @chart.route('/progress_view/<client_id>', methods=["GET", "POST"]) @chart.route('/progress_view', methods=["GET", "POST"]) @login_required def progress_view(): # client_id = request.args.get('client_id') client_id = session['client_id'] client_id = int(client_id) # from sqlalchemy import select # sth = select(ProgressNote.unitname, ProgressNote.staffname, ProgressNote.notedate, ProgressNote.notebody).where(ProgressNote.client_id == client_id) # results = db.session.execute(sth) results = db.session.query(ProgressNote).filter(ProgressNote.client_id == client_id).all() return render_template('progress_view.html', notes=results, client_id=client_id) @chart.route('/progress_test', methods=["GET", "POST"]) @login_required def progress_test(): client_id = 3 from sqlalchemy import select sth = select(ProgressNote.unitname, ProgressNote.staffname, ProgressNote.notedate, ProgressNote.notebody).where(ProgressNote.client_id == client_id) results = db.session.execute(sth) return render_template('progress_view.html', notes=results, client_id=client_id)
模板代码
<div id="logview"> <table style="border: 0px solid black; border-collapse: collapse; width:999px; margin-left: auto; margin-right: auto;"> <tbody> {% for note in notes %} <tr style="border: 1px solid black; border-collapse: collapse;" > <td style="border: 1px solid black; border-collapse: collapse;"><b>Unit Name:</b> <br />{{ note.unitname }}</td> <td style="border: 1px solid black; border-collapse: collapse;"><b>Entered By:</b> <br />{{ note.staffname }}</td> <td style="border: 1px solid black; border-collapse: collapse;"><b>Note Date:</b> <br />{{ note.notedate }}</td> </tr> <tr style="border: 1px solid black; border-collapse: collapse;" > <td colspan="5" style="text-align: left; border: 1px solid black; border-collapse: collapse;"><pre>{{ note.notebody}}</pre></td> </tr> <tr style="border: 0px solid black; border-collapse: collapse;" > <td colspan="5" style="border-collapse: collapse;"> <br /> <br /> </td> </tr> {% endfor %} </tbody> </table> </div>
模型代码
class ProgressNote(UserMixin, db.Model): rec_id = db.Column(db.Integer, primary_key=True) client_id = db.Column(db.Integer) chart = db.Column(db.String(80)) unitname = db.Column(db.String(255)) staffname = db.Column(db.String(255)) notedate = db.Column(db.DateTime) notebody = db.Column(db.String)
验证Session中
client_id的实际值
在progress_view路由中添加日志,确认Session中的client_id是否与数据库中存在的记录匹配:@chart.route('/progress_view', methods=["GET", "POST"]) @login_required def progress_view(): client_id = session['client_id'] client_id = int(client_id) # 打印日志验证 print(f"Session获取的client_id: {client_id}") # 统计匹配的记录数 record_count = db.session.query(ProgressNote).filter(ProgressNote.client_id == client_id).count() print(f"匹配的进度笔记数量: {record_count}") results = db.session.query(ProgressNote).filter(ProgressNote.client_id == client_id).all() return render_template('progress_view.html', notes=results, client_id=client_id)如果记录数为0,说明当前
client_id在ProgressNote表中无对应数据,或client_id的类型与数据库存储不匹配。检查
client_id字段类型一致性
确认ProgressNote模型中client_id的db.Integer类型,与数据库表中该字段的实际存储类型完全一致。若数据库中是字符串类型,转成int查询会导致匹配失败。统一查询语法并处理结果集
在progress_view中使用与progress_test相同的select语法,并调用.fetchall()确保结果转为可迭代列表:from sqlalchemy import select sth = select(ProgressNote.unitname, ProgressNote.staffname, ProgressNote.notedate, ProgressNote.notebody).where(ProgressNote.client_id == client_id) results = db.session.execute(sth).fetchall()模板添加调试输出
在模板中添加调试代码,确认notes是否为空:<div>调试信息:进度笔记数量 {{ notes|length }}</div> <div id="logview"> <!-- 原有表格代码 --> </div>
内容的提问来源于stack exchange,提问作者Thomas Altfather Good

