콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
1 회신
7800 화면

Suppose I have 2 models:

modela.py :

    start_date = fields.Date('Date working')

modelb.py:

    name = fields.Many2one('hr.employee', 'Employee')

    start_datetime = fields.Datetime('Datetime working')


In modelb.py, I created 3 records via field name and start_datetime:

Employee Name              Datetime working

A                                        10/18/2021    07.00

B                                        10/18/2021    08.00

C                                        10/18/2021    09.00

In modela.py, I selected via field start_date: 10/18/2021

and want to load ALL records of modelb.py which have: 10/18/2021 without going through time, via the function button using @onchange('Date')

I'd tried to convert the start_datetime from Datetime to Date in modelb.py First like this:

        import dateutil.parser

        start_datetime = fields.Datetime('Datetime working')

        get_date = dateutil.parser.parse(start_datetime).date()


But I got Errors this way.

Note: I want to keep the format of the start_datetime field as Datetime.

So how to get/load Date from the Datetime field in Odoo 13?

Please help!

Thank you!

아바타
취소
작성자

Finally, I'd solved my requirement. 

Using combine and timedelta to add an extra day as two conditions in the domain of @onchange.




베스트 답변

Hi,

The input to the dateutil.parser.parse() function should be a string.
Or you can use 'date()' function to convert datetime.datetime object to date object. 

Eg: 

date_new = datetime_date.date(), where datetime_date is a datetime object.

Regards.

아바타
취소
작성자

Hi,
Thank you for your support. After following your suggestion:
start_datetime = fields.Datetime('Datetime working')
date_new = start_datetime.date()

I got an error like that:
....
[Previous line repeated 317 more times]
RecursionError: maximum recursion depth exceeded while calling a Python object

What am I doing wrong?
Please help!
Thank you!

Hi,
You should first define the field as a Date field and compute its value using a function.
Please try this:
start_datetime = fields.Datetime('Datetime working')
date_new = fields.Date('Date working', compute='_compute_date_new', store=True)

@api.depends('start_datetime')
def _compute_date_new(self):
for rec in self:
rec.date_new = rec.start_datetime.date()

관련 게시물 답글 화면 활동
2
1월 24
2611
2
6월 22
4552
2
3월 22
8635
2
6월 18
6096
2
11월 16
13715