Add search_context for specific model on a Reference field

In a model I have a Reference field to link different records from different models. This works as expected. But now I want to filter down the list of records for a specific model.

So when a user select as Model ‘project.work’ (Work Effort) I want to filter out all the ‘project.work’ records which has state ‘done’. Because I don’t need them. But for other models this should not happen because some of them don’t have a field state and will fail.

Is this possible? And if so, any example available?

Thanks.

Yes, you should build a domain that depends on the target module. There is an example implemented on the sale_compaint module.

Hope it helps!

It worked perfectly! Thanks!

1 Like

Hi, I know this post is quite old, but I would like to know if it’s possible to build a constraint depending on the target model of a reference field, but on a search_context instead of a domain as the post says,
Thanks.

Reference fields have a search_context attribute but it is the same context for any target model.