跳至内容
菜单
此问题已终结
1 回复
7000 查看

hello odoo community,,

i have field 'production date' , 'duration' field and i need calculate 'expiration date' 

this is my module

from odoo import fields,models


class MyMarketProduct(models.Model):
_name = 'market.products'

def _compute_expiry(self):
self.production_date+self.valid_durity

name = fields.Char()
product_country = fields.Char()
image = fields.Binary()
production_date = fields.Date()
desc = fields.Char()
valid_durity = fields.Date()
expiration_date = fields.Date(compute=_compute_expiry)
形象
丢弃
最佳答案

Hello Omar,

You can calculate with new field for duration like how many month,week,year etc.. instead of date field for duration.

You can do it like following :

 duration = fields.Char('Duration')
duration_unit = fields.Selection([('month','Month'),('week','Week'),('day','Day'),('year','Year')])

Your calculation should be like following :

from dateutil.relativedelta import relativedelta

class MyMarketProduct(models.Model):
_name = 'market.products'

def _compute_expiry(self):

if self.duration_unit=='month':
expiry_date=self.production_date + relativedelta(months = int(self.duration))
elif self.duration_unit=='week':
expiry_date=self.production_date + relativedelta(weeks = int(self.duration))
elif self.duration_unit=='day'
expiry_date=self.production_date + relativedelta(days = int(self.duration))
elif self.duration_unit=='year'
expiry_date=self.production_date + relativedelta(years = int(self.duration))

self.expiration_date=expiry_date

Thanks,

形象
丢弃
相关帖文 回复 查看 活动
0
3月 21
6243
3
7月 20
20419
2
1月 19
6465
1
12月 18
2668
1
4月 18
3403