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
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 ?