How to Implement any() and all() Functions in Odoo?
Overview
Python’s built-in functions any() and all() are used for evaluating iterables (like lists or tuples) based on Boolean logic.
Function | Returns True if... |
any() | At least one element is True |
all() | All elements are True |
These are highly useful for validating multiple conditions concisely in Odoo development.
✅ Syntax
any(iterable)
all(iterable)
Each item in the iterable is evaluated as a Boolean (True or False).
Example: Basic Python
any([False, True, False]) # ➝ True
all([True, True, True]) # ➝ True
all([True, False, True]) # ➝ False
🧠 Realistic Odoo Examples
✅ any() – Check if at least one product is active
python
CopyEdit
products = self.env['product.template'].search([])
has_active = any(prod.active for prod in products)
if has_active:
_logger.info("There is at least one active product.")
✅ all() – Check if all partners have email addresses
partners = self.env['res.partner'].search([])
all_have_email = all(partner.email for partner in partners)
if not all_have_email:
_logger.warning("Some partners are missing email addresses.")