The video has shown to get the following items :order.id, order.customer.first_name to display on the html/webpage.
But, how to get Product title from the orderitem_set__product prefetch_related field for displaying it via template.html?
I have tried: order.orderitem_set.product.title…, but no use!
I too would like to know! In the HTML template I’ve tried:
{{ order.title }}
{{ order.product.title }}
{{ order.orderitem.product.title }}
{{ order.orderitem_set.product.title }}
I’ve also tried to simply pull the unit_price from the OrderItem model with:
{{ order.orderitem.unit_price}}
and
{{ order.orderitem_set.unit_price}}
but even that doesn’t work. Possibly something we don’t know about HTML?
i tried shortly.
but dont know how to inspect really a queryset.
anybody knows?
anyhow: every order has several orderitems, where each orderitem has one product with a title.
so i assume we have to somehow iterate over the orderitems.
i tried:
does not give an exception, but also no output of titles…
you can access products inside template using this code:
<ul>
{% for order in orders %}
<h4>Order_id: {{ order.id }} - Customer_name: {{order.customer.first_name}} - payment: {{order.payment_status}}</h4>
<span>Order_items:</span>
<ul>
{% for item in order.orderitem_set.all %}
<li>title: {{item.product.title}} - unit_price: {{item.product.unit_price}} </li>
{% endfor %}
</ul>
{% endfor %}
</ul>```
inside the object we have structures like this:
orders = [
{
‘id’: 1,
‘placed_at’: datetime.datetime(2022, 1, 1, 12, 0, 0),
‘payment_status’: ‘C’,
‘customer’: {
‘id’: 123,
‘name’: ‘John Doe’,
# Other customer fields
},
‘orderitem_set’: [
{
‘id’: 456,
‘quantity’: 2,
‘product’: {
‘id’: 789,
‘name’: ‘Product 1’,
# Other product fields
}
},
# Other OrderItem objects
]
},
# Other Order objects
]