2

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)
autusgo
  • 21
  • 6
  • Sounds like you were on the right track with a LineItem model. What was the problem with that? – ChidG Jan 28 '20 at 04:08
  • I would also and have in the past used the LineItem model to manage a product per line of invoice which will have a relationship to the product and relationship to the Factura which will be like the Document level object If you insist on using manytomany for products, you can use a custom many to many table using the "through" attribute and creating the intermediary model to manage qty price etc https://docs.djangoproject.com/en/3.0/ref/models/fields/#django.db.models.ManyToManyField.through – Artisan Jan 28 '20 at 05:42
  • I'm sorry I am not able to answer more quickly but for some reason stackoverflow.com is blocked from my work, I wouldn't know why. To answer @ChidG, The problem with LineItem is that I have to go to LineItem to fill that information and then go to Invoice to pick it up. If there's a way to unify everything under Invoice, it would be great. Maybe is something that is done when creating the views, I don't know, I'm just guessing. – autusgo Jan 29 '20 at 01:16
  • Answering @Omar, I'm not insisting in using ManyToManyField, if there's an easier way to do it I'm willing to try. I'll check the link you sent me and let you know if there's something that works for me. Thank you! – autusgo Jan 29 '20 at 01:16
  • 1
    You could use an inline formset to create an invoice containing multiple line items https://docs.djangoproject.com/en/3.0/ref/forms/models/#inlineformset-factory. Otherwise yes, that's the way Django works - you need to create the parent model before you can create the children, and combining those into view can be tricky. – ChidG Jan 29 '20 at 02:38

0 Answers0