This question has been flagged
2 Replies
7281 Views

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.

Avatar
Discard

Hi, did you found a solution for this ?

Author

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

Author

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.

Author

...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

Best Answer

 Try Domain in field define

Avatar
Discard
Best Answer

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

Avatar
Discard
Author

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).