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

编程入门 行业动态 更新时间:2024-10-26 02:24:48
本文介绍了刷新另一个表中的字段[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]

本文发布于:2023-10-15 00:49:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1492746.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   Django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!