djangomultiple-forms

How to make and submit multiple Django forms?


I got this two models, and I would like to attach multiple "Detalle compra" in one page, and send them all together. I saw this behavior in the administrator, but I would like to do it.

class Compra(models.Model):
     provedor = models.ForeignKey(Provedor)
     fecha = models.DateTimeField(auto_now_add=True)

class DetalleCompra(models.Model):

    compra = models.ForeignKey(Compra)
    producto = models.ForeignKey(Producto)
    precio_compra = models.DecimalField(max_digits=11, decimal_places=2)
    cantidad = models.DecimalField(max_digits=15,decimal_places=4)

Solution

  • I would take a look at Django formsets. It's exactly the behavior you are looking for I believe.

    https://docs.djangoproject.com/en/dev/topics/forms/formsets/