Friday, September 25, 2020

Re: Stuck with Calculated feild

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 

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

    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

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

    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

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

    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

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

    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

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

    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

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/68f3c6fd-192e-40a1-9a1b-0f7e095ee5bao%40googlegroups.com.

No comments:

Post a Comment