Skip to Content
Menu
You need to be registered to interact with the community.
This question has been flagged
2 Odgovori
1246 Prikazi

Here is my logic for fetching records from my Odoo Online DB and feeding the domain filters after I get them as POST request from the API that I'm building as a wrapper around Odoo External API.

common = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(url))uid = common.authenticate(database, username, password, {})

# connects to the db to facilitate CRUD operationsmodel = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url))

def datetime_to_valid_format(value):   
​try:       
​string_to_datetime = datetime.strptime(value, "%Y-%m-%d %H:%M:%S")
​datetime_as_valid_string_for_odoo = string_to_datetime.strftime("%m/%d/%Y %H:%M:%S")       
​return datetime_as_valid_string_for_odoo       
​except ValueError:       
​return value

def fetch_records(model_name, req_body = None):   
​if not req_body or req_body == None: #fetch all records        
​return model.execute_kw(database, uid, password, model_name, 'search_read', [])   
​else: #apply domain filter       
​# the request body for my API has the following keys: 'fieldname', 'operator', 'value'
​if 'value' in req_body.keys():           
​req_body['value'] = datetime_to_valid_format(req_body['value'])

​​domain_filter = tuple(req_body[key] for key in ['fieldname', 'operator', 'value'] if key in req_body)

​​try:
​res = model.execute_kw(database, uid, password, model_name, 'search_read',  [] if len(domain_filter)==0 else [[domain_filter]], {key: req_body[key] for key in ['limit', 'offset'] if key in req_body})           
​return res
        except Exception as e:
             return {'error': str(e)}, 400

My concern is the following: the domain filtering works absolutely fine on attributes other than datetime. But when I try applying filter concerning a datetime attribute. It doesn't work :(

For Example:

[[('active', '=', 'True')]] -> Works

[[('__last_update', '>=', '2024-01-22 16:54:07')]] -> Doesn't Work

Even though when I try converting it to %m/%d/%Y %H:%M:%S format as mentioned in one of the posts:

[[('__last_update', '>=', '01/22/2024 16:54:07')]] -> Doesn't work either

Avatar
Opusti

Hmmm i thought the domain for date field should be date or datetime object
like
[('date_start', '<=', contract.date_end)] where "contract.date_end" will return date or datetime object ?

Best Answer

Hi Naman,

ISO 8601 format (%Y-%m-%d %H:%M:%S) is working for me like that:

datetime_filter = '2024-05-01 00:00:00'
domain = [('create_date', '>=', datetime_filter)]

fields = ['name', 'partner_id', 'date_order', 'state']
result = models.execute_kw(db, uid, password,
    'sale.order', 'search_read',
    [domain],
    {'fields': fields})


Avatar
Opusti
Avtor Best Answer

No, It has to be coerced into a string certainly.

Avatar
Opusti
Related Posts Odgovori Prikazi Aktivnost
1
maj 25
1132
1
apr. 25
2444
1
apr. 25
3138
1
apr. 25
810
4
mar. 25
5467