For the purpose of this example consider the following table which has a parent row iterating a list of "orders" and each "order" has several child rows iterating a list of "items" for a given "order".
The list of "orders" and "items" are contained in separate tables joined together when we render our template with a query:
def order_items_template():
return render_template('order_items_template.html',
order_items_query = db.session.query(orders)
.join(items, orders.id == items.order_id)
.all())
Note below that we are also using a bootstrap collapsible accordion to display parent/child rows.
{% for order in orders %}
<tr data-toggle="collapse" data-target="#{{ order.number }}" class="clickable">
<td>{{ order.number }}</td>
</tr>
{% for item in order %}
<tr class="no-border collapse" id="{{ order.number }}">
<td>{{ item.name }}</td>
</tr>
{% endfor %}
{% endfor %}
Additionally we are joining the two tables with an sqlalchemy ForeignKey relationship:
class items (db.Model):
id = db.Column('id', db.Integer, primary_key = True)
item = db.Column(db.String(100))
order_id = db.Column(db.Integer)
class orders (db.Model):
id = db.Column('id', db.Integer, primary_key = True)
orders = db.Column(db.String(100))
item_id = db.Column(db.Integer, db.ForeignKey('items.order_id'), nullable=False)
item_relashionship = relationship("items")
Although the parent rows are displayed the data in the rows is not. Does anyone see where the problem lies?
edit: by changing the "child" jinja2 loop from {{ % for item in order % }} to {{ % for item in order.items % }} as suggested below no data is returned and the bootstrap accordion is "broken" and no longer collapses.
For testing purposes, adding {{ order.item_relashionship.name }} placeholder to the "parent loop" returns the name of an item matching an order_id which is obviously wrong.
What we want is the corresponding list of items for each order. This must have something to do with the relationship between the two tables but I can't figure out what is missing.