On 11/04/2022 2:48 pm, tech george wrote:
Hello Mike,

Unfortunately it's not calculating the fields as I wanted, instead it's calculating as below;
reorder_level * unit_price + quantity

Try using brackets like this ...

self.total_price = (self.reorder_level + self.quantity) * self.unit_price

Although I might use an error preventer. Perhaps like this ...

if self.unit_price and (self.reorder_level or self.quantity):
    self.total_price = (self.reorder_level + self.quantity) * self.unit_price



Which is wrong because I'm getting less amount.

Regards

On Mon, 11 Apr 2022, 01:56 Mike Dewhirst, <mi...@dewhirst.com.au> wrote:





-- (Unsigned mail from my phone)



    -------- Original message --------
    From: tech george <techgeor...@gmail.com>
    Date: 11/4/22 03:50 (GMT+10:00)
    To: django-users@googlegroups.com
    Subject: Calculated Fields

    Hello,

    I am trying to calculate fields directly from a model but no luck.
    I want to get total_price from quantity, reoder_level and unit
    price. My code is abelow, Please advise.

    class Stock(models.Model):
        unit_price = models.DecimalField(max_digits=10,
    decimal_places=2, default='0', blank=True, null=True)
        quantity = models.IntegerField(default='0', blank=True, null=True)
        total_price = models.DecimalField(max_digits=10,
    decimal_places=2,default=1)
        reorder_level = models.IntegerField(default='0', blank=True,
    null=True)


        def save(self, *args, **kwargs):
            self.total_price = self.quantity + self.reorder_level *
    self.unit_price
            super(Stock, self).save(*args, **kwargs)


    What am i my doing wrong

    If it was my code, I would extract the calculation into a model
    method and call it from the save method as you are doing.

    That would make it easier to unit-test and perhaps discover that
    there should be parens around the addition.

    You also need to call save on your model to execute the code.

    Apart from that, at first glance your code should work.



    Regards,

-- 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 view this discussion on the web visit
    
https://groups.google.com/d/msgid/django-users/CADYG20HBcMMGq6QodO0v%2BeZVmfS%2Bmr4aCSro5FZeeRLFdQztnQ%40mail.gmail.com
    
<https://groups.google.com/d/msgid/django-users/CADYG20HBcMMGq6QodO0v%2BeZVmfS%2Bmr4aCSro5FZeeRLFdQztnQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.
-- 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 view this discussion on the web visit
    
https://groups.google.com/d/msgid/django-users/6253606d.1c69fb81.ac7f2.09bcSMTPIN_ADDED_MISSING%40gmr-mx.google.com
    
<https://groups.google.com/d/msgid/django-users/6253606d.1c69fb81.ac7f2.09bcSMTPIN_ADDED_MISSING%40gmr-mx.google.com?utm_medium=email&utm_source=footer>.

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CADYG20Ec5fq2WJLsuTiaCLycc%2B%2B%3DM7iscYVf1PbvfyDEqhtPow%40mail.gmail.com <https://groups.google.com/d/msgid/django-users/CADYG20Ec5fq2WJLsuTiaCLycc%2B%2B%3DM7iscYVf1PbvfyDEqhtPow%40mail.gmail.com?utm_medium=email&utm_source=footer>.


--
Signed email is an absolute defence against phishing. This email has
been signed with my private key. If you import my public key you can
automatically decrypt my signature and be sure it came from me. Just
ask and I'll send it to you. Your email software can handle signing.

--
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/e6094b5b-ee18-a521-3ae3-7dfb7a4686b0%40dewhirst.com.au.

Attachment: OpenPGP_signature
Description: OpenPGP digital signature

Reply via email to