在Odoo 10中添加截止日期的月份数

2024-10-01 11:28:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创建一个可以这样做的模块:inputdate=“2017年2月27日”,nomonths=2,所以outputdate必须是“2017年4月27日”。inputdate和nomonths是两个必须填写的字段,而outputDate只是odoo中的一个“只读”字段。在

class DateGenerate(models.Model):
     _name = "studentmanagement.dategenerate"

    inputdate = fields.Date()
    nomonths = fields.Integer(required=True)
    outputdate = fields.Date(readonly=True)

    @api.onchange('inputdate','nomonths')
    def add_month(self):
       for record in self:
          dt = fields.Datetime.to_string(record.inputdate)
          inpYear = datetime.strptime(dt,"%Y")
          inpMonth = datetime.strptime(dt,"%m")
          inpDay = datetime.strptime(dt,"%d")
          outYear = inpYear + int((inpMonth + record.nomonths - 1)/12)
          outMonth = (inpMonth + record.nomonths - 1) % 12 + 1
          record.outputdate = datetime.date(outMonth, inpDay, outYear)

XML

我根据互联网上的来源和解释编写代码,但它不起作用并导致错误: enter image description here

我从这些链接中阅读代码和方向

https://docs.python.org/2/library/datetime.htmlhttps://github.com/odoo/odoo/blob/10.0/odoo/fields.py#L1504


Tags: odooselftruefieldsdatetimedatedtrecord
1条回答
网友
1楼 · 发布于 2024-10-01 11:28:57

要在日期/日期时间对象上添加/花费几个月,我建议使用python包:dateutil.relativedeltahttp://dateutil.readthedocs.io/en/stable/relativedelta.html

基本用法:

from datetime import date, datetime
from dateutil.relativedelta import relativedelta

print date.today() + relativedelta(months=+3)
print datetime.today() + relativedelta(months=+3)

print date.today() - relativedelta(months=+3)
print datetime.today() - relativedelta(months=+3)

您的问题同时引用了datetimedate对象,因此包含了这两个对象的示例。在

希望这有帮助。在

相关问题 更多 >