Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
1 Odpovědět
16970 Zobrazení

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

Avatar
Zrušit

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")

Nejlepší odpověď

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.

Avatar
Zrušit
Autor

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.

Autor

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 Odpovědi Zobrazení Aktivita
3
čvn 25
649
1
led 25
17640
1
led 20
3680
1
pro 19
6041
3
dub 18
4994