Stuck with Calculated feild

29 views
Skip to first unread message

eankomah

unread,
Sep 23, 2020, 1:24:05 AM9/23/20
to Django users
Hi all am stuck at getting total_price Calculated in my model

class inventory(models.Model):
    def __str__(self):
        return self.name

    category = models.ForeignKey(Category, null=True, on_delete = models.SET_NULL)
    Supplier = models.ForeignKey(Supplier, null=True, on_delete = models.SET_NULL)
    product = models.ForeignKey(Product, null=True, on_delete = models.SET_NULL)
    quantity = models.FloatField()
    unit_price = models.FloatField()
    total_price = models.FloatField(total=('unit_price' * 'quantity'))
    selling_price = models.FloatField()
    date_created = models.DateTimeField(auto_now_add=True) 


Thanks

dr neyx de godlove

unread,
Sep 23, 2020, 2:01:04 AM9/23/20
to django...@googlegroups.com
hi there you can create another method for that class that calculates the total as


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


--
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/4ab25477-99fa-4de4-a774-667d336235e6n%40googlegroups.com.

Kasper Laudrup

unread,
Sep 23, 2020, 5:19:11 AM9/23/20
to django...@googlegroups.com
Hi eankomah,

Completely unrelated, but:>     unit_price = models.FloatField()
Don't use floats for money. It will give you all kinds of problems with
rounding errors etc.

Assuming this is just a hobby project, it's probably fine, but otherwise
consider looking into something like:

https://github.com/django-money/django-money

Just an advice.

Kind regards,

Kasper Laudrup

Harish Thiyagharajan

unread,
Sep 23, 2020, 10:43:19 AM9/23/20
to django...@googlegroups.com
I think you mailed me instead of mailing someone

--
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,
Sep 23, 2020, 11:13:32 AM9/23/20
to django...@googlegroups.com
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

Frederic Salvetat

unread,
Sep 25, 2020, 9:56:12 AM9/25/20
to Django users

Hi Eankomah,

You have several solution 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 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 

Frederic Salvetat

unread,
Sep 25, 2020, 9:56:26 AM9/25/20
to Django users
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 
Reply all
Reply to author
Forward
0 new messages