Skip to Content
Меню
Вам необхідно зареєструватися, щоб взаємодіяти зі спільнотою.
Це запитання позначене
1 Відповісти
16635 Переглядів

My requirement is 

hire_date = fields.Date; 

calculate_start = fields.Date;

If hire_date  is after calculate_start ,the date of calculate_start should be equal hire_date

hr_employee_base.py

from odoo.tools import format_time
from datetime import date, datetime, timedelta, time
from time import time
import datetime
import time
import calendar

##### the hire_date;resign_date;calcucate_start  must be fields.Date

hire_date = fields.Date(string="Hire Date")

calculate_start = fields.Date(string="Start Date", store=True, compute='_get_calculate_date')


@api.onchange('hire_date')
def _get_calcuate_date(self):
    firstday = datetime.datetime.strptime(self.hire_date, "%Y-%m-%d")
    secondday = datetime.datetime.strptime(self.calculate_start, "%Y-%m-%d")
    if firstday > secondday:
          self.calculate_start = self.hire_date

File "/opt/bitnami/apps/odoo/lib/odoo-13.0.post20191110-py3.7.egg/odoo/addons/hr/models/hr_employee_base.py", line 110, in _get_calcuate_date
    firstday = datetime.datetime.strptime(self.hire_date, "%Y-%m-%d")
TypeError: strptime() argument 1 must be str, not datetime.date

Аватар
Відмінити

To avoid the type error re-write the following lines as below

firstday = datetime.datetime.strptime(str(self.hire_date), "%Y-%m-%d")

secondday = datetime.datetime.strptime(str(self.calculate_start), "%Y-%m-%d")

Найкраща відповідь

You don't need to convert the date into the datetime again as Odoo returns the date in datetime.date format, so you don't need to convert it again.

Just try this:

if self.hire_date > self.calculate_start:
    self.calculate_start = self.hire_date ​   ​    ​   ​     ​   ​    ​   ​

In previous versions, Odoo used to returns the date into the string format, so you had to convert it into datetime.

Аватар
Відмінити
Автор

Thank you for your answer,but actually I tried it at first and display the below error

File "/opt/bitnami/apps/odoo/lib/odoo-13.0.post20191110-py3.7.egg/odoo/addons/hr/models/hr_employee_base.py", line 110, in _get_calculate_date

if self.hire_date > self.calculate_start:

TypeError: '>' not supported between instances of 'datetime.datetime' and 'bool'

You have to make sure both the fields have value selected. Add following code:

if (if self.hire_date and

self.calculate_start:) and self.hire_date > self.calculate_start:

This condition will make sure if both the fields have value, then compare it.

Автор

Thank you very much,it works now.If you dont mind,one more question please,how to set the specific date like "2022-12-31" as default,not the default date as today?

You can either pass the date string in the default or pass the date in the time.strftime function

1: default="'2022-12-31'"

2: default="time.strftime('2022-12-31')"

Related Posts Відповіді Переглядів Дія
1
січ. 25
17295
1
січ. 20
3436
1
груд. 19
5648
3
квіт. 18
4775
1
трав. 16
6354