I need to filter the records that can be selected on a reference field. I can limit the model that can be selected but I have no idea how to apply domain or limit the record ids once a model was selected.
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- CRM
- e-Commerce
- Accounting
- Inventory
- PoS
- Project management
- MRP
This question has been flagged
Try Domain in field define
Hi,
You can add the dynamic domain to apply the domain on the records after selecting the model.
Adding a example to explain it.
add a onchange function on the reference field and return the domain of on the selected model's fields
product_ref= fields.Reference(selection=[('product.template', 'product.template'), ('product.product', 'product.product')])
@api.onchange("product_ref")
def _return_domain(self):
return { 'domain': {'product_ref': [('list_price', '>', 10)]} }
P.S. --> In the domain passed field "list_price" is present in product.template and product.product
I see your logic here although my problem is that it only takes effect during the selection of the reference model. After you Save and refresh the page, the domain is not enforced anymore when you try to change the record (unless you change the model again).
Hi, did you found a solution for this ?
Haven't found any direct answer to it. Now I am facing another problem as to how to create a domain to limit the model that can be selected. It might be best to create a dynamic selection field instead
My method to limit the model is to create another object (reference.object) that has the following fields: name (function field to return the model name based on model_id), model_id (m2o field relating to ir.model)
Then, my reference field selection points to the records in the reference.object. The end result, is that whatever records I added to my reference.object will be part of the model that can be selected in my reference field.
...however, I still don't have the solution to limit the records that can be selected from a reference field, either by using domain filter or by function