Skip to Content
Menu
This question has been flagged
1 Reply
6005 Views

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!

Avatar
Discard
Author

Finally, I'd solved my requirement. 

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




Best Answer

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.

Avatar
Discard
Author

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

Related Posts Replies Views Activity
2
Jan 24
939
2
Jun 22
2619
2
Mar 22
6622
2
Jun 18
4695
2
Nov 16
11671