Ensure_one method in Odoo - Odoo 17 Technical Slides

CelineGeorge1 258 views 7 slides Aug 05, 2024
Slide 1
Slide 1 of 7
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7

About This Presentation

The ensure_one method is a simple but powerful utility to enforce that operations are performed on a single record in Odoo. It is used to ensure that the recordset contains exactly one record. If the recordset contains zero or more than one record, it raises an error.


Slide Content

ensure_one method in Odoo Enterprise

Introduction Enterprise The ensure_one method is a simple but powerful utility to enforce that operations are performed on a single record in Odoo. It is used to ensure that the recordset contains exactly one record. If the recordset contains zero or more than one record, it raises an error. By using ensure_one, developers can write more robust and error-resistant code, ensuring that methods behave as expected when dealing with recordsets.

Enterprise ensure_one() is one of the very common ORM methods in Odoo which is used to handle the singleton error in cases when only one is expected to carry out the operation. The ensure_one method is typically defined in the models.py file of the Odoo 17 framework as follows def ensure_one(self): """Verify that the current recordset holds a single record. :raise odoo.exceptions.ValueError: ``len(self) != 1`` """ try: # unpack to ensure there is only one value is faster than len when true and # has a significant impact as this check is largely called _id, = self._ids return self except ValueError: raise ValueError("Expected singleton: %s" % self)

Enterprise We can just illustrate with a small example. Lets inherit ‘sale.order’ and ‘sale.order.line’ models and just create methods to print the sale order lines while changing the Customer(partner_id). class SaleOrder(models.Model): _inherit = 'sale.order' @api.onchange('partner_id') def _onchange_partner_id(self): if self.order_line: order_line_ids = self.order_line order_line_ids.print_order_line_ids() class SaleOrderLine(models.Model): _inherit = 'sale.order.line' def print_order_line_ids(self): print(self.id)

Enterprise Here, when an onchange happens for the partner_id field, the method _onchange_partner_id() gets called which calls the print_order_line_ids() method of sale.order.line. When there are multiple sale order lines, we get the Singleton error message like for example ValueError: Expected singleton: sale.order.line(<NewId origin=12>, <NewId origin=13>, <NewId origin=17>) That is, to call the print_order_line_ids(), there are 3 sale order lines with IDs 12, 13 and 17 got assigned to the variable order_line_ids . To avoid this, we use the ensure_one() method

Enterprise Now, just re write the method print_order_line_ids() as class SaleOrderLine(models.Model): _inherit = 'sale.order.line' def print_order_line_ids(self): self.ensure_one() print(self.id) If the method is called with an empty recordset or with multiple records, it raises an error. The program will be stopped before calling the print function and thus ensures the method only works if the condition is true.

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