简体   繁体   English

为什么我的查询仅在我的sqlalchemy flask应用程序中显示一个变量,而不是所有变量?

[英]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.

相关问题 为什么我的flask-sqlalchemy查询失败 - Why is my this flask-sqlalchemy query failing (Flask)为什么我的 HTML 中的 Jinja 块没有获取从 SQLAlchemy 查询传递的所有表条目? - (Flask) Why doesn't the Jinja block in my HTML get all the table entries passed from a SQLAlchemy query? 为什么我的flask-sqlalchemy 删除查询失败 - Why is my flask-sqlalchemy delete query failing 在我的 Tkinter gui 中,我能否让我的选项菜单只显示在一个选项卡上而不是所有三个选项卡上? - Can I have my options menu display only on one tab rather than all three in my Tkinter gui? 为什么我的 flask 应用程序无法处理多个客户端? - Why my flask app can't handle more than one client? 如何仅将我的 csv 文件加载到我的数据库中一次(flask-sqlalchemy)? - how to load my csv file into my database only one time (flask-sqlalchemy)? 为什么我的SQLAlchemy query.flter仅适用于某些属性? - Why my SQLAlchemy query.flter works only on some attribute? 为什么我的过滤器不适用于python SQLAlchemy Flask? - Why is my filter not working on python SQLAlchemy Flask? 为什么我的 Flask-SQLAlchemy 更新查询没有改变我的表内容 - Why does my Flask-SQLAlchemy update query not change my table content HTML 显示在实时服务器上,但不在我的 Flask 应用程序上呈现。 只有索引显示在烧瓶上 - HTML showing on live server, but not rendering on my flask app. Only the index is showing on flask
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM