How to Use Mapped Function in Odoo - Odoo 17 Slides

CelineGeorge1 1,745 views 17 slides Sep 04, 2024
Slide 1
Slide 1 of 17
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17

About This Presentation

The Mapped function returns a collection of field values. It takes a string value as an argument representing the column name and retrieves all possible values of that particular column from all the records in the recordset.


Slide Content

How to use mapped function in Odoo Enterprise

Introduction Enterpr ise The Mapped function returns a collection of field values. It takes a string value as an argument representing the column name and retrieves all possible values of that particular column from all the records in the recordset.

Mapped() Enterprise The mapped() function plays a crucial role in code optimization by eliminating user-defined loops. It provides a collection of field values. The argument it accepts is a string representing the column name, and it retrieves all possible values from the recordset.

Code Enterprise orders = self.env['sale.order'].search([]) names = orders.mapped('name') Explanation orders = self.env['sale.order'].search([]) self.env['sale.order']: It will accesses the sale.order model in Odoo. .search([]): This method searches for records in the sale.order model. its m eaning it retrieves every sales order available.

Code Enterprise Explanation names = orders.mapped('name') orders: This variable holds all the sales order records retrieved from the search method. .mapped('name'): The mapped method is used to collect the values of a specified field from the recordset.

M apped function Enterprise In Odoo, the mapped function is a useful method offered by the Odoo ORM (Object-Relational Mapping) to streamline the extraction of specific fields from a recordset, converting them into a list. Additionally, it can be used to invoke a method on each record within the recordset and collect the results

Mapped function Enterprise The mapped function can be employed in various ways: Basic Mapped Function Utilizing mapped with Relations Employing mapped with Methods Applying mapped with Multiple Fields

1. Base Mapped Function Enterprise The mapped function is used to extract field values from a recordset. For example, if you have a recordset of partners and you need to obtain a list of their names, we can use mapped function

1. Base Mapped Function Enterprise partner_names = self.env['res.partner'].search([]) names = partner_names.mapped(name) In this example: self.env['res.partner'].search([]) retrieves all partner records. partners.mapped(name) extracts the name field from each partner and returns a list of names.

2.Using mapped with Relations Enterprise The mapped function can also navigate relationships. For instance, if you have a recordset of sales orders and need to obtain a list of all product names within those orders, can use mapped function.

2.Using mapped with Relations Enterprise sale_orders = self.env['sale.order'].search([]) product_names = sale_orders.mapped('order_line.product_id.name') Explanation Retrieves all sale order records. Navigates through the sale orders to the order lines, then to the products, and finally extracts the product names.

3. Using mapped with Methods Enterprise The mapped function can also be employed to call methods on each record in a recordset. For example, if your model has a get_display_name method and you want to apply it to all records in the recordset, mapped functionality

3.Using mapped with Methods Enterprise display_names = partners.mapped('get_display_name') It will calls the get_display_name method on each partner record and returns a list of results.

4. Using mapped with Multiple Fields Enterprise Use mapped to obtain a list of dictionaries when you need to extract multiple fields simultaneously. It can be done with the help of mapped functionality.

4.Using mapped with Multiple Fields Enterprise partners = partners.mapped(lambda partner: {'name': partner.name, 'email': partner.email}) creates a list of dictionaries where each dictionary contains the name and email of a partner.

Enterprise Conclusion The mapped function within Odoo serves as a robust and adaptable tool, streamlining the extraction of data from recordsets, navigating relationships, and invoking methods on records. It aims in minimizing the necessity for excessive code to iterate over recordsets and extract or modify data, thereby enhancing the cleanliness and efficiency of code.

For More Info. Check our company website for related blogs and Odoo book. Check our YouTube channel for functional and technical videos in Odoo. Enterprise www.cybrosys.com