1👍
No offence, but this code is far from being correct.
Besides you’ve got many errors that you might want to remove.
Errors:
formprodingreso.is_valid()
is never called- inside
for i in range(5)
you use a class as if it was an instance (ProductosIngresos.SubtotalP
) clean
method in form has to be outside the Meta block
I believe what you want inside the loop is:
producto_ingreso = ProductosIngresos()
producto_ingreso.idIngreso = ingreso # better change to producto_ingreso.ingreso
producto_ingreso.Concepto=request.POST.get("Concepto"+str(i), "") # producto_ingreso.concepto
producto_ingreso.SubtotalP=request.POST.get("SubtotalP"+str(i), "") # producto_ingreso.subtotal_p
producto_ingreso.IvaP=request.POST.get("IvaP"+str(i), "")
producto_ingreso.TotalP=request.POST.get("TotalP"+str(i), ""))
producto_ingreso.save()
To make it cleaner, you can make this king of logic overridding the save() method of ModelForm. Or use inline formsets.
Confusion:
- Model FKs are objects in Django, not integers. Better name them like condominio instead of
idCondominio
- Decimal columns (subtotal, iva, total) should be declared as deciaml i.e.
models.DecimalField(max_digits=10, decimal_places=2)
- clean method is intended for cross field validation (more than one field). Only one field should be validated by
clean_numero
f.e.
Over complication:
- models have ID/PK by default, no need to explicit them (referenced as
self.id
orself.pk
)- model
unicode
function is not giving any info - clean and ValidationError are superflous: modelform checks if attributes are requiered automatically
- model
Convention errors:
- attributes are always written_in_lowercase (SubtotalP -> subtotal_p)
I would seriously try to fix all of those if you dont want the developers maintaining your code hate you and make some voodoo on you.
Source:stackexchange.com