Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

257
Vistas
Django: showing many to many additional fields in template

I have following problems when trying to show the additional field of the 'trough table' of a many-to-many relationship templates. my model.py:

class Product(models.Model):
  productName =  models.CharField(max_length=500)
  price = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
  def __str__(self):
    return self.productName 
  def get_absolute_url(self):
    return reverse('itake:productList')

class Order(models.Model):
   orderID = models.CharField(max_length=100, blank=True, null=True)
   products = models.ManyToManyField(Product,related_name="order_item", through="OrderItem")
   def __str__(self):
      return self.orderID

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

my views.py:

def orderDetails(request,order_id):
    order = get_object_or_404(Order,pk=order_id)
    products = order.products.all()
    context = {'order': order,'products':products}
    return render(request, 'itake/orderDetails.html', context)

my templates:

 {% for products in products %}
     <p>{{products.productName}} |
        {{products.price}} |
        {{products.orderQuantity}}</p>
  {% endfor %}

However, it can only list the product name and price, the orderQuantity is not showed in the webpage.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda