在Django中将产品和数量添加到发票

2024-06-28 20:09:19 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,我对Django和Python非常陌生,我正在努力制作一个非常简单的发票。 我有三节课供应商产品发票。产品和供应商的工作很好,我能够填写它们,并将它们保存在数据库中,现在的问题是我需要填写发票,选择供应商、产品(一个或多个),并填写每个产品的数量。 我似乎不知道该怎么做:给每个选择的产品加上一个数量(我不知道怎么做),然后计算总数(我知道我应该乘以价格和数量,然后把它们相加,但我不知道该怎么做,怎么保存)。我尝试添加一个行项目类,但也无法使其工作

我正在使用Python3.7和Django3.0

我正在发送到目前为止我编写的代码。(以防万一:Proveedor=供应商,Producto=产品,Factura=发票,cantidad=数量,precio=价格)

提前谢谢你

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)

Tags: selftrue数量产品models发票nulllength