Home > Blockchain >  How can I calculate discount fields from another table's fielsd
How can I calculate discount fields from another table's fielsd

Time:10-21

I have a model like this:

class InvoiceItem(models.Model):
   book = models.ForeignKey(Book, on_delete=models.PROTECT)
   invoice = models.ForeignKey(Invoice, on_delete=models.PROTECT, related_name='items')
   title = models.CharField(max_length=100, null=True, blank=True)
   price = models.IntegerField(null=True, blank=True)
   discount = models.IntegerField(blank=True, default=0)
   totalprice = models.IntegerField(null=True, blank=True)
   count = models.IntegerField(null=True, blank=True)

and I want to calculate discount from book's discount table How can I do it? should I calculate it in models?

CodePudding user response:

Here we can create property for override models fields like this (without override save() method)

class InvoiceItem(models.Model):
    book = models.CharField(max_length=100)
    invoice = models.PositiveIntegerField()
    title = models.CharField(max_length=100, null=True, blank=True)
    og_price = models.IntegerField(null=True, blank=True)
    sell_price = models.IntegerField(null=True, blank=True)
    discount = models.IntegerField(blank=True, default=0)
    discounted_price = models.IntegerField(blank=True, default=0)
    count = models.IntegerField(null=True, blank=True)

    @property
    def discounted_price(self):
        return ((self.og_price*self.discount)/100)

    @property
    def sell_price(self):
        return ((self.og_price - self.discounted_price))

with override save() method

class InvoiceItem(models.Model):
    book = models.CharField(max_length=100)
    invoice = models.PositiveIntegerField()
    title = models.CharField(max_length=100, null=True, blank=True)
    og_price = models.IntegerField(null=True, blank=True)
    sell_price = models.IntegerField(null=True, blank=True)
    discount = models.IntegerField(blank=True, default=0)
    discounted_price = models.IntegerField(blank=True, default=0)
    count = models.IntegerField(null=True, blank=True)

    @property
    def dis_price(self):
        print((self.og_price*self.discount)/100)
        return ((self.og_price*self.discount)/100)

    @property
    def selling_price(self):
        print(self.og_price - self.dis_price)
        return (self.og_price - self.dis_price)

    def save(self, *args, **kwargs):
        self.sell_price = self.selling_price
        self.discounted_price = self.dis_price
        super(InvoiceItem, self).save(*args, **kwargs)

Output

You need to fill only those fields

enter image description here

enter image description here

NOTE: here i changed two fields for data adding in database

book = models.CharField(max_length=100)
invoice = models.PositiveIntegerField()

CodePudding user response:

If you want to calculate it before saving, or when it enter form admin panel or from any place you can override Save method in django model

Or you can calculate from view for example and insert it on saving

Save method example:

class MyModel(models.Model):
      ...
      def save(self, *args, **kwargs):
         here you can get field value and insert it in any field you want
  • Related