Search code examples
pythonormodooodoo-13

Odoo 13 - Search method - use of 'order' attribute for the model's related fields


I have 2 models more like the example below

Model 1 - invoice

invoice_num = fields.Char(String='Invoice Number')
payment_method = fields.Char(String='Payment Method')
payment_date = fields.Datetime(String='Invoice Payment Date')
paid_by_id = fields.Many2one('user_details', String='Paid by user ID')
.
.
.

Model 2 - user_details

first_name = fields.Char(String='First Name')
last_name = fields.Char(String='Last Name')
age = fields.Integer()
.
.
.

I am trying to fetch all the records from the invoice model with the following search conditions

Domain filters - payment_method = 'Cash' & paid_by_id.age > 25

Order - paid_by_id.first_name desc

Offset - 200

Limit - 10

For the above, the raw query would be like

SELECT * FROM invoice inv
JOIN user_details ud ON inv.paid_by_id = ud.id
WHERE inv.payment_method = 'Cash' and ud.age > 25
ORDER BY ud.first_name
OFFSET 200 LIMIT 10;

I tried to write ORM for the same like

records = request.env['invoice'].sudo().search([('payment_method', '=', 'Cash'), ('paid_by_id.age', '>', 25)], order='paid_by_id.first_name desc', offset=200, limit=10)

But, I get the below error-

'Invalid "order" specified. A valid "order" specification is a comma-separated list of valid field names (optionally followed by asc/desc for the direction)'

So, how to fetch records using 'order' attribute for the model's related fields?


Solution

  • The order must be a comma-separated list of valid field names, optionally followed by an ASC or DESC direction.

    You can't use dotted names (fields of relational fields).