{% for order in orders %}
<li>{{order.customer.last_name}} - {{ order.orderitem_set.product.unit_price }}</li>
{% endfor %}
class Product(models.Model):
title = models.CharField(max_length=255)
slug = models.SlugField()
description = models.TextField()
unit_price = models.DecimalField(max_digits=6,
decimal_places=2)
inventory = models.IntegerField()
last_update = models.DateTimeField(auto_now=True)
collection = models.ForeignKey(Collection,
on_delete=models.PROTECT)
promotions = models.ManyToManyField(Promotion)
class Order(models.Model):
placed_at = models.DateTimeField(auto_now_add=True)
customer = models.ForeignKey(Customer,
on_delete=models.PROTECT)
class OrderItem(models.Model):
order = models.ForeignKey(Order, on_delete=models.PROTECT)
product = models.ForeignKey(Product, on_delete=models.PROTECT)
quantity = models.PositiveSmallIntegerField()
unit_price = models.DecimalField(max_digits=6,
decimal_places=2)
How can I get products unit_price in browser, last_name shows but when I am trying to use django reverse relation created item the value don't showing
CodePudding user response:
As an order can consist of multiple items you would have to iterate over the set (the reverse relation):
{% for item in order.orderitem_set.all %}
{{ item.product.title }}
{% endfor %}
Habe a look at the documentation for querying reverse relations.