How to Use Filtered Function in Odoo 17 - Odoo Slides

CelineGeorge1 1,320 views 16 slides Sep 20, 2024
Slide 1
Slide 1 of 16
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

About This Presentation

In Odoo, the filtered function is a method provided by the Odoo ORM (Object-Relational Mapping) to filter through records in a recordset according to particular conditions. It enables us to implement a filter on a recordset and fetch the records that satisfy the designated criteria.


Slide Content

How to use filtered function in Odoo Enterprise

Introduction Enterpr ise The filtered function will provide the recordsets that meet the criteria or condition.

Filtered() Enterprise In Odoo, the filtered function is a method provided by the Odoo ORM (Object-Relational Mapping) to filter through records in a recordset according to particular conditions. It enables us to implement a filter on a recordset and fetch the records that satisfy the designated criteria.

Filtered() Enterprise filtered() provides a collection of records (recordset) that fulfill the specified condition within an existing recordset. Utilizing the built-in Python lambda function with filtered() allows for the extraction of the necessary records. filtered() aims in streamlining and enhancing the code by eliminating unnecessary loop conditions to meet the criteria.

Example Enterprise variable = self.env['model.name'].filtered(lambda r: r.name == name) Explanation self.env['model.name']: The model named 'model.name' within the Odoo environment (self.env). It's essentially querying the database to retrieve records from the specified model. So there we can specify the corresponding model name.

Example Enterprise Explanation .filtered(lambda r: r.name == name) .filtered(): This method is used to filter records based on a condition. lambda r: r.name == name: This is a lambda function that specify the filtering condition. It iterates over each record r in the recordset and checks if the value of the name field of the record (r.name) is equal to the value stored in the name variable.

Example Enterprise Explanation variable = The filtered records that meet the specified condition are assigned to the variable. Line in the code retrieves entries from the 'model.name' model where the content of the name field corresponds to the content stored in the name variable, and allocates these refined entries to the variable.

Filtered function Enterprise The filtered function can be employed in various ways: Basic Filtered Function Using filtered with Complex Conditions The filter function can be utilized in two ways: the fundamental approach and simultaneously employing complex conditions.

1. Basic Filtered Function Enterprise The filtered function is employed to filter records within a recordset according to specific conditions. Imagine you possess a recordset of partners, and your objective is to filter out the active partners. Use the specified code.

Example Basic Filtered Function Enterprise partners = self.env['res.partner'].search([]).filtered(lambda p: p.active) Explanation In this example: self.env['res.partner'].search([]) - retrieves all partner records. .filtered(lambda p: p.active) - applies a filter to the recordset, filtering only the partners for which the active field is True.

2.Using filtered with Complex Conditions Enterprise Filtered can be applied with more complex conditions by employing lambda functions or multiple criteria. For example, if you need partners whose names commence with 'A' and who are active, refer the code.

Code Enterprise filtered_orders = self.env['sale.order'].search([]) .filtered(lambda o: o.state == 'draft' or (o.amount_total > 1000 and o.partner_id.category_id.name == 'VIP'))

Using filtered with Complex Conditions Enterprise In this example, the code searches for all sale orders in the 'sale.order' model and filters them based on two conditions: Either the state of the order must be 'draft'. Or the order's total amount must be greater than 1000 and the partner must belong to the 'VIP' category. The filtered orders are then stored in the filtered_orders variable.

Example Enterprise products = self.env['product.product'].search([]).filtered(lambda p: p.type == 'product' and p.qty_available > 0) Explanation In this example, the code searches for all products in the 'product.product' model and filters them based on two conditions: The type of the product must be 'product'. The quantity available for the product must be greater than 0. The filtered products are then stored in the products variable.

Enterprise Conclusion The filtered feature within Odoo offers a method for implementing filters on recordsets, fetching those records that satisfy particular conditions. It enables the composition of neat code for data queries, diminishing the requirement for manual iteration and filtering. Through utilizing filtered, one can effectively handle expansive datasets and effortlessly extract desired information.

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