Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
1 Odpowiedz
7749 Widoki

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!

Awatar
Odrzuć
Autor

Finally, I'd solved my requirement. 

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




Najlepsza odpowiedź

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.

Awatar
Odrzuć
Autor

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

Powiązane posty Odpowiedzi Widoki Czynność
2
sty 24
2530
2
cze 22
4468
2
mar 22
8594
2
cze 18
6036
2
lis 16
13624