Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
1 Rispondi
7743 Visualizzazioni

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
Abbandona
Autore

Finally, I'd solved my requirement. 

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




Risposta migliore

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
Abbandona
Autore

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

Post correlati Risposte Visualizzazioni Attività
2
gen 24
2527
2
giu 22
4465
2
mar 22
8594
2
giu 18
6035
2
nov 16
13621