Skip to content Skip to sidebar Skip to footer

Reading From Joined Query In Flask-sqlalchemy

After successfully joining two db tables, I'm trying to read the data from the 2nd table by addressing the first. I'm addressing opinion.topic_name from my jinja2 template, but not

Solution 1:

This query:

opinions = Opinion.query
                    .filter_by(party_id=form.party.data)
                    .filter_by(topic_id=form.topic.data)
                    .join(Topic)
                    .all()

will return a list of Opinion models and since in your relationship in Topic model, you defined it as:

opinions = db.relationship(Opinion, backref='topic')

Then, in your jinja2 template, to access Topic.topic_name, you should do:

<h1>{{ opinion.topic.topic_name }}</h1>

Post a Comment for "Reading From Joined Query In Flask-sqlalchemy"