首先,我对Django和Python非常陌生,我正在努力尝试让一个非常简单的发票工作,我有3个类。提供者, 产品介绍 和 发票. 产品和供应商工作得很好,我能够填写它们,它们被保存在数据库中,现在的问题是我需要填写一张发票,选择一个供应商,产品(一个或多个),并填写每个产品的数量.我似乎找不到如何做:添加一个数量到每个选定的产品(我不知道如何做到这一点),并计算总数(我知道我应该乘以价格和数量,然后将它们相加,但我不知道在哪里做,如何保存它)。我试着添加了一个 LineItem 类,但我也无法使其工作。
我在使用 Python 3.7和Django 3.0。
我把我目前写的代码发过来。(以备不时之需。Proveedor=供应商, Producto=产品, Factura=发票, cantidad=数量, precio=价格)
先谢谢你
模型.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)