Reputation: 163
I want to create a ticket (model Ventas) with his products (model Venta_detalle)
My models are:
class Ventas(models.Model):
metodo_pago = models.CharField(max_length=80)
fecha = models.DateTimeField(auto_now_add=True,auto_now=False)
cajero = models.ForeignKey('auth.user')
cliente = models.ForeignKey(Clientes,blank=True,null=True)
total = models.DecimalField(max_digits=10,decimal_places=2)
class Venta_detalle(models.Model):
venta = models.ForeignKey(Ventas)
producto = models.ForeignKey(Productos)
cantidad = models.IntegerField()
def __unicode__(self):
return self.producto.descripcion
class Productos(models.Model):
codigo = models.CharField(max_length=80)
descripcion = models.TextField()
precio_compra = models.DecimalField(max_digits=10,decimal_places=2)
precio_venta = models.DecimalField(max_digits=10,decimal_places=2)
existencia = models.IntegerField()
impuesto = models.ForeignKey(Impuestos,blank=True,null=True)
imagen = models.ImageField(upload_to='productos')
def __unicode__(self):
return self.descripcion
I need to create a lot of Venta_detalle objects with a product,quantity and Ventas ID in the same template.
For example: When we add a ObjectInline(admin.TabularInline) in admin panel to show two models with foreign key.
My view only have:
def VentasCrear(request):
if request.method == "POST":
form = VentaForm(request.POST)
if form.is_valid():
venta = form.save(commit=False)
venta.cajero = request.user
venta.save()
else:
form = VentaForm()
return render(request, 'ventas.html', {'form': form})
Upvotes: 0
Views: 1128
Reputation: 131
En español: Veo que lo que quieres es guardar una sola Venta y en ella Guardar muchos detalles de esa venta, como los son los poductos y eso. Como lo tienes esta bien pero le falta una parte, en la vista seria de esta forma
I am also learning Python & Django, but I think what you want is to save a single Sale and in it save many many details of that sale, such as Productos. You're on the right track, but here is how to modify the view.
def VentasCrear(request):
if request.method == "POST":
# cuando llega el formulario los recibimos con sus respectivos prefijos (prefix) asi:
#primero el formulario de Ventas
form = ventaform(request.POST, prefix="venta_form")
#ahora recibimos el formulario de los detalles
form2 = venta_detalle_form(reques.POST, prefix="venta_detalle_form")
if form.is_valid() and form2.is_valid(): # si los formularios son validos
venta = form.save(commit=False)
venta.cajero = request.user
venta.save()
# en este punto ya tienes la ventaguardada ahora procedes con los detalles
detalle = form2.save(commit=False)
detalle.venta = venta # esta es la llave foranea de lo que guardamos arriba
detalle.save()
else: # este se cumpliria si es un GET
formventa = ventaform(prefix="venta_form")
formdetalle = venta_detalle_form(prefix="venta_detalle_form")
context = { 'form' : formventa, 'form2' : formdetalle }
return render(request, 'ventas.html', context)
Upvotes: 2
Reputation: 4512
"How to Save 2 forms in one view with Django?"
In your GET
, you initialize your forms with prefixes:
venta_form = VentaForm(prefix='venta_form')
venta_detalle_form = VentaDetalleForm(prefix='venta_detalle_form')
context = { 'venta_form' : venta_form, 'venta_detalle_form' : venta_detalle_form }
return render(request, 'ventas.html', context)
Then in your POST
:
venta_form = VentaForm(request.POST, prefix='venta_form')
venta_detalle_form = VentaDetalleForm(request.POST, prefix='venta_detalle_form')
and there you go.
Upvotes: 1