[Fixed]-Django: showing many to many additional fields in template

1👍

orderQuantity is a property of OrderItem, not of Product.

Try:

class OrderItem(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE,null=True)
    order = models.ForeignKey(Order,on_delete=models.CASCADE, null=True, related_name='items')
    orderQuantity = models.IntegerField(default=10)
    def __str__(self):
      return self.order.orderID + '-' +self.product.productName

And in the template:

{% for item in order.items %}
<p>
    {{ item.product.productName }} |
    {{ item.product.price }} |
    {{ item.orderQuantity }} 
</p>
{% endfor %}

And maybe you want to change the models.CASCADE of the model, as this will cause to delete the product if the order is deleted.

Leave a comment