Skip to Content
Menu
This question has been flagged
4 Replies
3495 Views


    Passwd_pin = fields.Char(string="Password", size=10, readonly=True)

    start_date = fields.Date(string="Start Date", readonly=True, default=fields.Date.today())

    end_date = fields.Date(string="End Date", required=False, default=fields.Date.today(days=365))

    state = fields.Selection([('draft','Draft'),('active','Active'),('disactive','Disactive')], string="State", default='draft')

Avatar
Discard
Best Answer

Hi, try like below:


from dateutil.relativedelta import relativedelta
def _compute_end_date(self):
       return fields.Date.today() + relativedelta(years=+1)
end_date = fields.Date(string="End Date", required=False, default=_compute_end_date)

Avatar
Discard
Best Answer

Hello Hadeel Khattab ,

Please find code in comment. 

I hope this will help you.

Thanks & Regards,
Email: odoo@aktivsoftware.com
Skype: kalpeshmaheshwari

Avatar
Discard

# import packages
from dateutil.relativedelta import relativedelta

@api.onchange("start_date")
def _onchange_start_date(self):
self.end_date = self.start_date + relativedelta(years=1)

OR

def _compute_end_date(self):
self.end_date = self.start_date + relativedelta(years=1)

You can use the above function as the compute function of required fields.

Best Answer

Hi,

You can try like below code.

from odoo.tools import date_utils

def next_year_date(self):
self.end_date = date_utils.add(self.start_date, years=1)

You can use the above function as the compute function of required fields.

Regards

Avatar
Discard
Best Answer

Hi,

You can add a constrains in your model as follows.

@api.constrains('start_date', 'end_date')
def _check_dates(self):
if any(self.filtered(lambda rec: rec.start_date > rec.end_date)):
raise ValidationError(_("Start Date must be before End Date.'"))

To know more about the constrains in Odoo, see: How To Add Constrains For A Field in Odoo


Thanks

Avatar
Discard