Hi Eankomah,
You have several solutions depending upon your need:

*>Le mercredi 23 septembre 2020 07:24:05 UTC+2, eankomah a écrit :*
*>Hi all am stuck at getting total_price Calculated in my model*
*>*
*>class inventory(models.Model):*
*>    def __str__(self):*
*>        return self.name*
*>  ...*
*>    total_price = models.FloatField(total=('unit_price' * 'quantity'))*
*>  ...*
*>Thanks*

First *dr neyx de godlove*'s answer is the more elegant if you do not need 
a dedicated db field.
You just need to remove the 'get_' prefix  in get_total_price :

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

(*@dr neyx de godlove*, thank I learned the use of property in django 
trying you solution)

Then if you really need a dedicated field, create it and override the save 
method in your model :
    total_price = models.FloatField()

    def save(self, *args, **kwargs):
        #change total_price
        # Transaction.save(update_fields['total_price'])
        self.total_price = self.unit_price * self.quantity
        super(Inventory, self).save(*args, **kwargs)


Best Regards
--
Frédéric Salvetat 
Le mercredi 23 septembre 2020 à 17:13:32 UTC+2, Kasper Laudrup a écrit :

> Hi Harish,
>
> On 23/09/2020 11.27, Harish Thiyagharajan wrote:
> > I think you mailed me instead of mailing someone
> > 
>
> No. If you look at the mail header, you can see this was sent to the 
> django-users mailing list.
>
> Kind regards,
>
> Kasper Laudrup
>

-- 
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/b9412f55-35c5-461e-b984-42b5a8e6e95cn%40googlegroups.com.

Reply via email to