我想创建一个可以这样做的模块: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)
我根据互联网上的来源和解释编写代码,但它不起作用并导致错误:
我从这些链接中阅读代码和方向
https://docs.python.org/2/library/datetime.htmlhttps://github.com/odoo/odoo/blob/10.0/odoo/fields.py#L1504
要在日期/日期时间对象上添加/花费几个月,我建议使用python包:
dateutil.relativedelta
。http://dateutil.readthedocs.io/en/stable/relativedelta.html基本用法:
您的问题同时引用了
datetime
和date
对象,因此包含了这两个对象的示例。在希望这有帮助。在
相关问题 更多 >
编程相关推荐