Django ORM Part 13 - Solution to actually print the products?

Love the course so far. One thing I’m struggling with though. In the exercise in Part 13, Mosh explains how to get the data for the last 5 orders, including customers (which I nailed, smug grin) and products (which I didn’t… but it makes sense now!).

What he doesn’t do though is show how to modify the template to actually display the products.

I’ve tried using a second for loop to print {{ orders.orderitem_set__product.title }} but that - perhaps unsurprisingly - doesn’t work. Anyone care to enlighten me?

{% for order in orders %}
< li >
{% for item in orderitem_set.all %}
{{ item.product.title }}
{% endfor %}
< /li >
{% endfor %}

1 Like

Hi, thank you @clementineRin for the suggestion.
I first tried to use orderitem_set but got an error because that is “not iterable”, so your adding of “.all” got me rid of that.
Anyway I had to add a reference to the order in the for loop to work:

{% for order in orders %}
<ul>
<li>{{order.id}} - {{order.placed_at}} - {{order.payment_status}}</li>
<li>{{order.customer.first_name}} - {{order.customer.email}}</li>
{%for item in order.orderitem_set.all%}
<li>id: {{item.id}} - Qty: {{item.quantity}} - Product: {{item.product.title}}</li>
{%endfor%}
</ul>
{% endfor %}

Regards!

3 Likes