Literal Eval in Odoo 17 - Odoo 17 Slides

CelineGeorge1 515 views 7 slides Oct 04, 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

Literals are the notations representing a fixed value in the code. Python supports various types of literals, such as numeric literals, string literals, Boolean literals, and more.


Slide Content

literal_eval in odoo 17 Enterprise

Introduction Enterprise Literals are the notations representing a fixed value in the code. Python supports various types of literals, such as numeric literals, string literals, Boolean literals, and more. The literal_eval function, found in the Python builtins module, is used to evaluate a string containing a l iteral structure. In simpler terms, it can convert a string representation of a Python data type (like an integer, list, or dictionary) back into the actual Python object. In Odoo, literal_eval is often used to safely evaluate strings containing Python expressions from user input or configuration files.

Enterprise The ast (Abstract Syntax Tree) module in Python is a library that provides tools for interacting with Python code itself by representing it as an abstract syntax tree. literal_eval is designed to execute arbitrary code if the input string isn't carefully validated. To use it , we need to import it from the ast module. from ast import literal_eval

Enterprise Consider the code from Odoo 17 addons crm module’s code. Here, assignment_domain is a field defined in the crm.team model and the method _constrains_assignment_domain just assigns the domain for the CRM Team. @api.constrains('assignment_domain') def _constrains_assignment_domain(self): for team in self: try: domain = literal_eval(team.assignment_domain or '[]') if domain: self.env['crm.lead'].search(domain, limit=1) except Exception: raise exceptions.ValidationError(_('Assignment domain for tea m %(team)s is incorrectly formatted', team=team.name))

Enterprise Here, the snippet domain = literal_eval(team.assignment_domain or '[]') just evaluates the string stored in the team.assignment from every CRM Team record. It returns true if the parameter is correctly a python literal. In the next statement, we need to keep the exceptional case if the literal_eval() is False. Here in this case, that result into the wrong domain assignment or the domain can be empty. So, the final except statement is written.

Enterprise l iteral_eval() can be U seful in case when we need to define custom filters and store them as strings in the database. These strings are safely parsed back into dictionaries using literal_eval. Used to convert the parsed filter criteria into a domain as we have seen in the previous example. So, Odoo ORM can use to search for records using this. Used for error handling

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