--(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 wrongIf 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.

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

Reply via email to