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!!!