News & Events

Dynamic Domain in Odoo 16

June 1, 2023
In Odoo 16, we can make use of domains to filter or restrict the records returned from the SQL query. It allows you to specify criteria that the documents must meet in order to be included in a particular operation, such as a search, read, write, or delete operation. But when we want to return a filter from a domain based on other relational fields, such as user input, we have to apply a dynamic domain. It can be computed at runtime based on certain conditions or variables. So let us see how Odoo 16 allows us to apply the dynamic domain in your Python code.

Let’s create a class and define fields in it.

from odoo import models, fields, api

class Product(models.Model):

_name = 'product.details’

name = fields.Char(string='Name', required=True)

product_id = fields.Many2one(‘product.product’, string = ‘Product’)

price = fields.Float(string='Price')

quantity = fields.Integer(string='Quantity')

product_type = fields.Selection([

        ('storable_product', 'Storable Product'),

        ('service', 'Service'),

        ('consumable', 'Consumable'),

    ], string='Product Type')

categ_id = fields.Many2one(‘product.category’, string = ‘Product Category’)

uom_id = fields.Many2one(‘uom.uom’, string= ‘Unit’)
  1. Based on the class created, we can call an on-change function to apply the domain dynamically.
@api.on change('product_type')

def on_category_change(self):

if self.categ_id:

return {'domain': {product_id : [(categ_id, '=',]}}
In this example, when the categ_id field changes, the on_category_change method is triggered. It returns a domain for the product field, only products with a matching categ_id value will be available for selection. Request Your Free Quote
  1. Dynamic Domains in Views: You can define dynamic domains directly in XML views. For example, you can use the attrs attribute to set dynamic domain conditions on fields based on the values of other fields.
<field name="uom_id"/>

<field name="product_id" attrs="{'domain': [(uom_id, '=', uom_id)]}"/>
In this example, the domain condition ['uom_id', '=', uom_id] will filter the options for the product_id field based on the selected value of the uom_id field. Make sure the uom_id field value is set before selecting the product_id 3. Compute Fields: You can also use computed fields to dynamically calculate a value based on certain conditions and apply a domain to other fields based on the computed value.
def _compute_product_domain(self):
for the product in self:
if product.categ_id:
domain = [('categ_id', '=',]
product.product_id = False
return {'domain': {'product_id': domain}}
In this example, the domain of the product_id field is computed based on the selected category. When the categ_id field is changed, the domain is set to only allow products that belong to the selected category (categ_id = The product_id field is reset to False to enforce the domain. These are the different ways of applying a dynamic domain to your model class in Odoo 16. Request Your Free Quote

Latest Posts

Experience Innovative Odoo Implementation Services at Gitex Summit 2023

September 28, 2023

Creating Notifications in Odoo 16 for Display

September 27, 2023

Members Module Features in Odoo 16

September 26, 2023

Create On-Site Payments & Picking With Odoo 16

September 19, 2023

Leave a Reply

Your email address will not be published. Required fields are marked *