コンテンツへスキップ
メニュー
この質問にフラグが付けられました
1 返信
7021 ビュー

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
6250
3
7月 20
20428
2
1月 19
6490
1
12月 18
2680
1
4月 18
3421