刷新另一个表中的字段[Django]

时间:2015-08-03 10:19:36

标签: django

我有这两个模型:

class probe(models.Model):

    serial=models.CharField("Serial Number",max_length=30,primary_key=True)
    clientID=models.ForeignKey(organisation)
    inst_date=models.DateField("Installation Date")
    exp_date=models.DateField("Expiration Date",blank=True)


    def save(self):
         if self.exp_date is None:
            self.exp_date=self.inst_date.replace(year=self.inst_date.year+1)
    super(probe,self).save()


    def isExpired(self):
        return self.exp_date<=datetime.date.today()
     isExpired.admin_order_field="exp_date"
     isExpired.boolean=True
     isExpired.short_description="Needs calibration"


     def __str__(self):              
         return self.serial

class calibration(models.Model):
    probe=models.ForeignKey(probe)
    date=models.DateField("Date of Calibration")
    isSent=models.BooleanField("Email sent?",default=False)

    def __str__(self):              
        return str(self.date)

    def save(self):
        self.probe.exp_date=self.date.replace(year=self.date.year+1)
        super(calibration, self).save()   

当我创建校准时,我希望更新探针的失效日期,如何实现我的模型以允许更新?

1 个答案:

答案 0 :(得分:1)

试试这个:

from dateutil.relativedelta import relativedelta

def save(self):
    self.probe.exp_date=self.date + relativedelta(years=1)
    self.probe.save()
    super(calibration, self).save()