Hi,

I think you are doing it right. I would remove the PurchaseOrderTotal model
however. The reason for this is that the purchase order itself should know
it's total and so on. Either you can add a signal on the save of the
PurchaseOrderItem - and update the PurchaseOrder total or the PurchaseOrder
could calculate the total when required via a method.

I think the easiest way would be to add a method that calculates the total:

@property
def subtotal(self):
    order_total = 0
    for item in self.purchaseorderitem_set.all():
        order_total += item.qty * item.unit_price
    return order_total

Now it is a property on the purchase order. So calling:
purchase_order.subtotal will get the total for the purchase order.

2 other things:
1. You can remove the amount in the PurchaseOrderItem and calculate the
total for the row when required (see my example above) - this of course
depends on WHEN you want to show the total. The same thing goes for
calculating the total on the PurchaseOrder. You could add a property the
same way I did for the PurchaseOrder in the PurchaseOrderItem class.
2. The names you have given your properties that are foreign keys are not
that good from an object oriented perspective. I would for example call the
invoice_number field on the PurchaseOrder as "invoice". The same goes for
the po_number_fk fields. Because when you assign them or when you want to
get a value from the invoice it would be easier to read -
purchase_order.invoice.id than purchase_order.invoice_number.id.

Hope that helps somewhat.

If you don't want to calculate the total each time (it all depends on the
usage), you can use signals to see when a model is saved/created and then
update the subtotal in the PurchaseOrder. See here for information about
signals: https://docs.djangoproject.com/en/2.0/topics/signals/

Andréas

2018-08-02 19:27 GMT+02:00 Alexander Joseph <alexander.v.jos...@gmail.com>:

> I've been working on a Purchase Order app but I'm getting a little
> confused how I'm going to put it all together.
>
> I have 3 models -
>
> class PurchaseOrder(models.Model):
>     po_number = models.IntegerField(default=get_po_number, unique=True)
>     po_date = models.DateField()
>     invoice_number = models.ForeignKey(Invoice, on_delete=models.CASCADE)
>     ....
>
>
> class PurchaseOrderItem(models.Model):
>     po_number_fk = models.ForeignKey(PurchaseOrder,
> on_delete=models.CASCADE)
>     qty = models.IntegerField()
>     unit = models.CharField(max_length=100, blank=True, null=True)
>     description = models.CharField(max_length=255)
>     unit_price = models.DecimalField(max_digits=6, decimal_places=2)
>     amount = models.DecimalField(max_digits=6, decimal_places=2)
>
>
> class PurchaseOrderTotal(models.Model):
>     po_number_fk = models.ForeignKey(PurchaseOrder,
> on_delete=models.CASCADE)
>     subtotal = models.DecimalField(max_digits=6, decimal_places=2)
>     tax = models.DecimalField(max_digits=6, decimal_places=2,
> default="7.82")
>     shipping = models.DecimalField(max_digits=6, decimal_places=2)
>     other = models.DecimalField(max_digits=6, decimal_places=2)
>     total = models.DecimalField(max_digits=6, decimal_places=2)
>
>
> the first (PurchaseOrder) holds information about the purchase order
> itself. ie. what the invoice number is, the vendor, etc.
> the second (PurchaseOrderItem) lists items in the purchase order to
> purchase
> the third (PurchaseOrderTotal) totals up the amounts from the items and
> adds tax etc. (I may not need this model.. I can probably put this info in
> the first model?)
>
>
> Does it look like I'm going about this in the right way or should I take
> away the third model and put those fields from the third model into the
> first model? How do I total up all prices for all items? I'm sure I'll need
> to do some sort of loop to total up all prices but where do I do that? In
> the form_valid fucntion? or do I override the save function and do it
> there? Thanks!
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/dc6a1cb9-0770-49d9-9c89-92d1f947e718%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/dc6a1cb9-0770-49d9-9c89-92d1f947e718%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAK4qSCfmDFAUMRVhmH-0ROeVpcfUUMwWuwaz%2Bxfd2SBhR5PQ4w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to