Calculated Fields

78 views
Skip to first unread message

tech george

unread,
Apr 10, 2022, 1:51:05 PM4/10/22
to django...@googlegroups.com
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

Regards,

Mike Dewhirst

unread,
Apr 10, 2022, 6:56:24 PM4/10/22
to django...@googlegroups.com




--
(Unsigned mail from my phone)

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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CADYG20HBcMMGq6QodO0v%2BeZVmfS%2Bmr4aCSro5FZeeRLFdQztnQ%40mail.gmail.com.

tech george

unread,
Apr 11, 2022, 12:50:04 AM4/11/22
to django...@googlegroups.com
Hello Mike,

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

Which is wrong because I'm getting less amount.

Regards

Mike Dewhirst

unread,
Apr 11, 2022, 1:49:50 AM4/11/22
to django...@googlegroups.com, tech george
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


To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CADYG20Ec5fq2WJLsuTiaCLycc%2B%2B%3DM7iscYVf1PbvfyDEqhtPow%40mail.gmail.com.


-- 
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.
OpenPGP_signature

Antonis Christofides

unread,
Apr 11, 2022, 2:05:02 AM4/11/22
to django...@googlegroups.com

As was found out with the conversation with Mike Dewhirst, the error appears to be because of wrong understanding of operator precedence. That's easy to correct with parentheses.

Except for that, I wouldn't do it that way; I wouldn't store the total price in the database, because it's likely to create problems. Models that have not (yet) been saved to the database will not have a total price. Models that have been loaded from the database, modified, but not (yet) updated in the database will have a wrong total price. Saving to the database with bulk operations or with plain SQL may result in no total price. These are all symptoms of storing redundant information in the database, which is something that should be avoided.

Instead, I would do this:

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)
    reorder_level = models.IntegerField(default='0', blank=True, null=True)

    @property
    def total_price(self):
        return (self.quantity + self.reorder_level) * self.unit_price

tech george

unread,
Apr 11, 2022, 4:59:01 AM4/11/22
to django...@googlegroups.com
Hello Antonis,

Thanks for the advice and the solution,

That solved my issue.

Thanks again.

Jet Ezra

unread,
Apr 11, 2022, 5:01:50 AM4/11/22
to django...@googlegroups.com
I think you can use the legendary BODMAS, and apply brackets
self.total_price = self.quantity+(self.reorder_level * self.unit_price)
That should work



--
jet

Ahmedrufai Otuoze

unread,
Apr 11, 2022, 9:17:36 AM4/11/22
to django...@googlegroups.com
Hi Mike, 

I can help you solve the problems.
I can clone it from your repo and we'll work on it...

What do you think?

harsh jain

unread,
Apr 13, 2022, 6:38:56 AM4/13/22
to Django users
Hi,
This course will help you surely - https://geekster.in/

Kasper Laudrup

unread,
Apr 13, 2022, 7:07:44 AM4/13/22
to django...@googlegroups.com
On 13/04/2022 07.20, harsh jain wrote:
> Hi,
> This course will help you surely - https://geekster.in/
>

Stop spamming. This is *not* the way to attract potential clients.

I would never ever consider using your company for anything and I hope
others feel the same.
OpenPGP_0xE5D9CAC64AAA55EB.asc
OpenPGP_signature

Ahmedrufai Otuoze

unread,
Apr 13, 2022, 11:03:25 AM4/13/22
to django...@googlegroups.com
This is not a spam. I just tok a course on that aspect and wanted to help. 
No strings attached. 

I may have gone against the policy and that's because I wasn't aware.
My intentions were genuine...

--
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...@googlegroups.com.

Kasper Laudrup

unread,
Apr 13, 2022, 1:33:04 PM4/13/22
to django...@googlegroups.com
On 13/04/2022 16.51, Ahmedrufai Otuoze wrote:
> This is not a spam. I just tok a course on that aspect and wanted to help.
> No strings attached.
>
> I may have gone against the policy and that's because I wasn't aware.
> My intentions were genuine...
>

You were not the one I was replying to so I don't understand why you are
replying like you were advertising this.

Harsh Jain has previously been spamming this mailing list and he's the
one I was replying to.

Kind regards,

Kasper Laudrup
OpenPGP_0xE5D9CAC64AAA55EB.asc
OpenPGP_signature

Ahmedrufai Otuoze

unread,
Apr 21, 2022, 3:47:05 PM4/21/22
to django...@googlegroups.com
My bad. I bypassed your message completely.

Have a great day!

--
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...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages