Skip to Content
Menu
You need to be registered to interact with the community.
This question has been flagged
1 Odgovori
6650 Prikazi

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)
Avatar
Opusti
Best Answer

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,

Avatar
Opusti
Related Posts Odgovori Prikazi Aktivnost
0
mar. 21
5791
3
jul. 20
19829
2
jan. 19
5979
1
dec. 18
2334
1
apr. 18
3060