本文介绍了刷新另一个表中的字段[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()创建校准时,我希望更新探头的失效日期,如何实现我的模型以允许这样做?
When I create a calibration, I want the expiry date of the probe to update, how can I implement my models to allow that?
推荐答案尝试一下:
from dateutil.relativedelta import relativedelta def save(self): self.probe.exp_date=self.date + relativedelta(years=1) self.probe.save() super(calibration, self).save()更多推荐
刷新另一个表中的字段[Django]
发布评论