Django - 默认字段值取决于其他字段值

时间:2021-03-25 17:10:34


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.

我在设置默认字段值时遇到问题。我想做的事?我希望类包中的价格是类Bill中priceNoTax的默认值。如您所见,所有三个类都是逻辑连接的。示例:帐户1有一个ID为1的软件包。此软件包的价格为100.帐户1的priceNoTax的默认值为100.如何做?我是相对较新的,所以我需要帮助。

models.py

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

非常感谢!!!

3 个解决方案

#1


5  

perhaps add this to your Bill class?

或许将此添加到您的Bill类?

def save(self, *args, **kwargs):
    if self.priceNoTax is None:
        self.priceNoTax = self.idAccount.idPackage.price
    super(Bill, self).save(*args, **kwargs)

#2


5  

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().

您仍然可以在{{bill.priceTax}}的模板中以相同的方式使用它。在代码中,您需要使用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装饰器来避免在代码中将其作为函数调用。

@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

有关更多信息,请参阅https://docs.djangoproject.com/en/2.0/topics/db/models/#model-methods

#3


-2  

@kichik answered how to display the default value using template tag, and I recommend to implement auto-calculation by javascript.

@kichik回答了如何使用模板标签显示默认值,我建议通过javascript实现自动计算。

Of course, you will have to validate user input or implement save() method as @Chris Curvey said.

当然,你必须验证用户输入或实现save()方法,如@Chris Curvey所说。

#1


5  

perhaps add this to your Bill class?

或许将此添加到您的Bill类?

def save(self, *args, **kwargs):
    if self.priceNoTax is None:
        self.priceNoTax = self.idAccount.idPackage.price
    super(Bill, self).save(*args, **kwargs)

#2


5  

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().

您仍然可以在{{bill.priceTax}}的模板中以相同的方式使用它。在代码中,您需要使用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装饰器来避免在代码中将其作为函数调用。

@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

有关更多信息,请参阅https://docs.djangoproject.com/en/2.0/topics/db/models/#model-methods

#3


-2  

@kichik answered how to display the default value using template tag, and I recommend to implement auto-calculation by javascript.

@kichik回答了如何使用模板标签显示默认值,我建议通过javascript实现自动计算。

Of course, you will have to validate user input or implement save() method as @Chris Curvey said.

当然,你必须验证用户输入或实现save()方法,如@Chris Curvey所说。