# Add search\_context for specific model on a Reference field

**URL:** https://discuss.tryton.org/t/add-search-context-for-specific-model-on-a-reference-field/2624
**Category:** Developer
**Created:** [April 23, 2020, 10:37am UTC](https://discuss.tryton.org/t/add-search-context-for-specific-model-on-a-reference-field/2624 "2020-04-23T10:37:51Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![edbo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@edbo](https://discuss.tryton.org/u/edbo)
#### Post date: [April 23, 2020, 10:37am UTC](https://discuss.tryton.org/t/add-search-context-for-specific-model-on-a-reference-field/2624/1 "2020-04-23T10:37:51Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![pokoli](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/pokoli/32/22_2.png) [@pokoli](https://discuss.tryton.org/u/pokoli)
#### Post date: [April 23, 2020, 10:51am UTC](https://discuss.tryton.org/t/add-search-context-for-specific-model-on-a-reference-field/2624/2 "2020-04-23T10:51:56Z")

</div>

> [@edbo](#):
>
> 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?

Yes, you should build a domain that depends on the target module. There [is an example implemented on the sale\_compaint](https://hg.tryton.org/modules/sale_complaint/file/4f609f29aa70/complaint.py#l125) module.

Hope it helps!

---

<div class="post-metadata">

### Author: ![edbo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@edbo](https://discuss.tryton.org/u/edbo)
#### Post date: [April 23, 2020, 12:37pm UTC](https://discuss.tryton.org/t/add-search-context-for-specific-model-on-a-reference-field/2624/3 "2020-04-23T12:37:10Z")

</div>

It worked perfectly! Thanks!

---

<div class="post-metadata">

### Author: ![Xavi](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/x/8e8cbc/32.png) [@Xavi](https://discuss.tryton.org/u/Xavi)
#### Post date: [November 19, 2024, 7:59am UTC](https://discuss.tryton.org/t/add-search-context-for-specific-model-on-a-reference-field/2624/4 "2024-11-19T07:59:59Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [November 19, 2024, 8:58am UTC](https://discuss.tryton.org/t/add-search-context-for-specific-model-on-a-reference-field/2624/5 "2024-11-19T08:58:16Z")

</div>

Reference fields have a [`search_context` attribute](https://docs.tryton.org/latest/server/ref/fields.html#trytond.model.fields.Reference.search_context) but it is the same context for any target model.
