跳至內容
選單
此問題已被標幟
1 回覆
7736 瀏覽次數

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
2521
2
6月 22
4464
2
3月 22
8591
2
6月 18
6029
2
11月 16
13616