order = Order.objects.select_related(‘customer’).prefetch_related(‘orderitem_set__product’).order_by(’-placed_at’)[:5]
Instance here : How do I display the Product Title?
order = Order.objects.select_related(‘customer’).prefetch_related(‘orderitem_set__product’).order_by(’-placed_at’)[:5]
Instance here : How do I display the Product Title?
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
]