[英]Why is my query only showing one variable in my sqlalchemy flask app rather than all?
Does anyone know why this query only shows stadium_name from Stadium and not all of Fixture as well? 有谁知道为什么这个查询只显示Stadium中的Stadium_name而不显示全部Fixture?
@app.route('/fixtures/', methods=['GET'])
@login_required
def fixtures():
fixtures = db.session.query(Fixture,Stadium.stadium_name).join(Stadium)\
.filter(Fixture.stadium_id==Stadium.id).order_by(desc(Fixture.fixture_dt))
return render_template('fixtures.html', fixtures = fixtures) #refer to template
It looks as though you may be overcomplicating your query. 看来您可能使查询复杂化了。 SQLAlchemy will use the relationships in your data model, as long as you have set them up correctly. 只要正确设置了SQLAlchemy,它们就会在数据模型中使用这些关系。 I'm guessing that you have a one-to-many model of Stadium to Fixture, so you should be able to do something like this: 我猜想您有一个“体育场到灯具”的一对多模型,因此您应该能够执行以下操作:
fixtures = Fixture.query.join(Stadium).order_by(desc(Fixture.fixture_dt))
Then, in your HTML file, you can access the fields using a dotted notation: 然后,在HTML文件中,您可以使用点分符号来访问字段:
{% for f in fixtures %}
<td>{{ f.name }}</td><td>{{ f.stadium.stadium_name }}</td>
{% endfor %}
Here I've assumed that you have set up relationships similar to the one-to-many example. 在这里,我假设您已经建立了类似于一对多示例的关系。 The 'stadium' is the backref from the Fixture to the Stadium. “体育场”是从灯具到体育场的后备箱。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.