型号:
class JobCycle(models.Model):
dateReceived = models.DateField(blank=True, null=True)
dueDate = models.DateField(blank=True, null=True)
dateOut = models.DateField(blank=True, null=True)
fee = models.IntegerField(blank=True, null=True)
def __str__(self):
return "Job Cycle" + str(self.id)
def save(self, *args,**kwargs):
if self.dateReceived and self.dueDate:
if self.dateReceived > self.dueDate:
raise ValidationError(
'Due date will never be greater than the received date')
super(JobCycle, self).save(*args,**kwargs)
我想进行计算,到期日永远不会超过接收日期。我想在模型中执行此操作
1条答案
按热度按时间ryevplcw1#
您可以覆写模型的保存()方法:
在
def __str__
(在同一级别)之前或之后添加:或者,您可以更正dueDate,而不是引发错误:
取决于所需的逻辑。
验证错误来自: