Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use mapped() in odoo 10

What is mapped and how use this in odoo 10? And how to use mapped and filter in Odoo 10? example

result = sum( order.order_line.filtered(
                lambda r: r.state != "state" ).mapped( "field_name" ) 
)

and multiply each value of field1 by other field2 in same table an return all sum.

like image 462
Karara Mohamed Avatar asked Sep 25 '17 14:09

Karara Mohamed


People also ask

What is use of mapped function in Odoo?

mapped(): applies the provided function to each record in the recordset, returns a recordset if the results are recordsets. The provided function can be a string to get field values. In your code the expression order.

What is self mapped Odoo?

In the above example odoo would have iterated through all of the records and returned the email from each record in the form of a list. So rather than looping through all records to obtain the same field from each record you can use mapped.

How to use filtered in odoo?

filtered() returns a set of records(recordset) which met the given condition on an existing recordset. We can use the built-in python function lambda (anonymous function) with filtered() to filter out the required records. In the above example, the variable partner_ids stores all the records from the table 'res.

How will you check whether the recordset is Singleton?

recordset. ensure_one() checks if it is a single record (singleton); if it's not, a ValueError exception is raised.


2 Answers

Is fully documented on Odoo docs:

mapped(): applies the provided function to each record in the recordset, returns a recordset if the results are recordsets. The provided function can be a string to get field values.

# returns a list of names
records.mapped('name')

In your code the expression order.order_line.filtered( lambda r: r.state != "state" ).mapped( "field_name" ) returns a list of field_name from order. Then sum python function do the sum.

like image 186
dani herrera Avatar answered Oct 06 '22 01:10

dani herrera


The filtered will return the recordsets that match the criteria (in your case, the order lines that the state isnt "state").

When you use mapped to the recordset, i will return a list with the field_name for each of the recordset, if the field is a many2one, it will remove duplicates.

Example, you have a recordset with a field called "quantity".

record 1: quantity = 5
record 2: quantity = 6
record 3: quantity = 10

after the filtered you may have:

res = sale.order.line(1,2,3,)

when applying mapped:

quantities_list = res.mapped('quantity') #[5, 6, 10]

so the sum will return 21 if they are float/int.

Hope it helps!

like image 35
dccdany Avatar answered Oct 06 '22 01:10

dccdany