Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - default field value depends on other field value


I have a problem to set a default field value. What I want to do?
I want that price in class Packages be a default value of priceNoTax in class Bill. As you can see, all three classes are logically connected.
Example: Account 1 has a package with id 1. Price of this package is 100. Default value of priceNoTax for Account 1 is 100.

How to do that? I am relative new at this, so I need help.

models.py

class Packages(models.Model):
     #other fields
     price = models.IntegerField(validators=[MinValueValidator(1)], verbose_name="Price of package")

class Account(models.Model):
     startDate = models.DateField(verbose_name="Start date")
     finishDate = models.DateField(verbose_name="Finish date")
     idPackage = models.ForeignKey(Packages, on_delete=models.CASCADE, verbose_name="Package")

class Bill(models.Model):
     date = models.DateField(default=datetime.now())
     tax = models.FloatField(default=0.20)
     priceNoTax = models.IntegerField()
     priceTax = models.FloatField(default=priceNoTax+(priceNoTax*tax))
     idAccount = models.ForeignKey(Account, on_delete=models.CASCADE, verbose_name="Account")

     def __str__(self):
         return self.date

Thanks a lot!!!

like image 287
notGenius Avatar asked Aug 21 '16 15:08

notGenius


2 Answers

Why do you need it to be a field? Do you see a reason where someone would want to change the total price without changing the price and tax to the corresponding values? If it doesn't really need to be a field, you can just make it a method.

class Bill(models.Model):
     date = models.DateField(default=datetime.now())
     tax = models.FloatField(default=0.20)
     priceNoTax = models.IntegerField()
     idAccount = models.ForeignKey(Account, on_delete=models.CASCADE, verbose_name="Account")

     def priceTax(self):
       return self.priceNoTax + (self.priceNoTax*self.tax)

     def __str__(self):
         return self.date

You can still use it the same way in templates with {{ bill.priceTax }}. In code you would need to use bill.priceTax().

This way, the price with tax should stay up-to-date no matter how the tax or price without tax changes.

You can also use @property decorator to avoid having to call it as a function in code.

@property
def priceTax(self):
   return self.priceNoTax + (self.priceNoTax*self.tax)

For more see https://docs.djangoproject.com/en/2.0/topics/db/models/#model-methods

like image 156
kichik Avatar answered Oct 19 '22 06:10

kichik


perhaps add this to your Bill class?

def save(self, *args, **kwargs):
    if self.priceNoTax is None:
        self.priceNoTax = self.idAccount.idPackage.price
    super(Bill, self).save(*args, **kwargs)
like image 24
Chris Curvey Avatar answered Oct 19 '22 05:10

Chris Curvey