问题
First of all, I am very new to Django and Python and I'm struggling trying to make an VERY simple invoice to work. I have 3 classes; Provider, Product and Invoice. Product and Provider work just fine, I am able to fill them out and they get saved in the database, the thing now is I need to fill an Invoice, selecting a Provider, Products (one or many) and filling the quantity for each product. What I can't seem to find out how to do is: add a quantity to each product selected (I have no clue how to do that) and calculate the total (I know I should be multiplying prices and quantities and then summing them up but I don't know where to do that and how to save it). I tried adding a LineItem class but I couldn't make it work either.
I'm using Python 3.7 and Django 3.0
I'm sending the code I wrote so far. (just in case: Proveedor=Provider, Producto=Product, Factura=Invoice, cantidad=quantity, precio=price)
Thank you in advance!
models.py
class Proveedor(models.Model):
apellido = models.CharField(max_length=200, null=True)
nombre = models.CharField(max_length=200, null=True)
cuit = models.CharField(max_length=13, null=True)
teléfono = models.CharField(max_length=200, null=True)
dirección = models.CharField(max_length=200, null=True)
def __str__(self):
return '{} {}'.format(self.nombre, self.apellido)
class Producto(models.Model):
UNIDAD_DE_MEDIDA = (
('Litro', 'Litro'),
('Kilo', 'Kilo'),
('Gramo', 'Gramo'),
('Cm3', 'Cm3'),
('Unidad', 'Unidad'),
)
nombre = models.CharField(max_length=200, null=True)
presentación = models.CharField(max_length=200, null=True)
unidad_de_medida = models.CharField(max_length=200, null=True, choices=UNIDAD_DE_MEDIDA)
precio_unitario = models.FloatField(null=True)
def __str__(self):
return '{} {}'.format(self.nombre, self.presentación)
class Factura(models.Model):
ESTADO = (
('Paga', 'Paga'),
('No Paga', 'No Paga'),
)
numero = models.IntegerField(null=True)
producto = models.ManyToManyField(Producto)
cantidad = models.IntegerField(max_length=200, null=True)
proveedor = models.ForeignKey(Proveedor, null=True, on_delete= models.SET_NULL)
fecha = models.DateTimeField(auto_now_add=True, null=True)
estado = models.CharField(max_length=200, null=True, choices=ESTADO)
def __str__(self):
return '{} {} {}'.format(self.fecha, self.numero, self.proveedor)
来源:https://stackoverflow.com/questions/59941669/adding-products-and-quantity-to-an-invoice-in-django