Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
1 ตอบกลับ
6994 มุมมอง

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,

อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
0
มี.ค. 21
6231
3
ก.ค. 20
20413
odoo/python invalidate_all() แก้ไขแล้ว
2
ม.ค. 19
6446
1
ธ.ค. 18
2665
1
เม.ย. 18
3377