跳至內容
選單
此問題已被標幟
1 回覆
6774 瀏覽次數

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
5900
3
7月 20
20014
2
1月 19
6086
1
12月 18
2413
1
4月 18
3112