# Retrieve rows conditionally

**URL:** <https://discuss.tryton.org/t/retrieve-rows-conditionally/5231>\
**Category:** Developer\
**Created:** [May 3, 2022, 1:57pm UTC](https://discuss.tryton.org/t/retrieve-rows-conditionally/5231 "2022-05-03T13:57:07Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![pumontjm](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/p/bbce88/32.png) [@pumontjm](https://discuss.tryton.org/u/pumontjm)\
**Post date:** [May 3, 2022, 1:57pm UTC](https://discuss.tryton.org/t/retrieve-rows-conditionally/5231/1 "2022-05-03T13:57:07Z")

</div>

Hello,

In different modules I need to display only the lines which concerns the logged in user.

example:  
A view returns all registered leave requests.  
User “A” logs in and opens this view. I want him to only have access to leave requests that concern him.

How can I do this?

I tried to do this directly in the “ **setup** ” function of a module but I can’t find how to filter my request or how to retrieve the “employee” context variable.  
“Transaction().context.get(‘employee’)” returns “None”

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:** [May 3, 2022, 2:41pm UTC](https://discuss.tryton.org/t/retrieve-rows-conditionally/5231/2 "2022-05-03T14:41:55Z")

</div>

Hello,

You need to add some [Record Rules](https://docs.tryton.org/projects/server/en/latest/topics/access_rights.html?highlight=Rule#record-rule) to restrict which records the user is allowed to see. Once you’ve defined the rule the trytond server will filter out the records that the user is not allowed to see.

Hope it helps!

---

<div class="post-metadata">

**Author:** ![pumontjm](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/p/bbce88/32.png) [@pumontjm](https://discuss.tryton.org/u/pumontjm)\
**Post date:** [May 3, 2022, 3:04pm UTC](https://discuss.tryton.org/t/retrieve-rows-conditionally/5231/3 "2022-05-03T15:04:35Z")

</div>

It helps me yes, I found examples in the code but it’s still a bit fuzzy. I may ask you for help again soon.

thank you 🙂

---

<div class="post-metadata">

**Author:** ![pumontjm](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/p/bbce88/32.png) [@pumontjm](https://discuss.tryton.org/u/pumontjm)\
**Post date:** [May 4, 2022, 10:07am UTC](https://discuss.tryton.org/t/retrieve-rows-conditionally/5231/4 "2022-05-04T10:07:00Z")

</div>

To create the registration rules I looked at the company module.

I think I need to adapt this:

```xml
<record model="ir.rule.group" id="rule_group_sequence_companies">
    <field name="name">User in companies</field>
    <field name="model" search="[('model', '=', 'ir.sequence')]"/>
    <field name="global_p" eval="True"/>
</record>
<record model="ir.rule" id="rule_sequence_companies1">
    <field name="domain"
        eval="[('company', 'in', Eval('companies', []))]"
        pyson="1"/>
    <field name="rule_group" ref="rule_group_sequence_companies"/>
</record>
<record model="ir.rule" id="rule_sequence_comapnies2">
    <field name="domain" eval="[('company', '=', None)]" pyson="1"/>
    <field name="rule_group" ref="rule_group_sequence_companies"/>
</record>

```

but I don’t know what to put in the “domain” fields.  
I also don’t know what to put in my python script.

---

<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:** [May 4, 2022, 12:09pm UTC](https://discuss.tryton.org/t/retrieve-rows-conditionally/5231/5 "2022-05-04T12:09:23Z")

</div>

> [@pumontjm](#):
>
> but I don’t know what to put in the “domain” fields.

You need to include the [domain](https://docs.tryton.org/projects/server/en/latest/topics/domain.html?highlight=domain#domain) that limites the records that the user is allowed to see.  
In your example, the user is just allowed to see the records that the company is in the list of companies the user is allowed to access.

---

<div class="post-metadata">

**Author:** ![pumontjm](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/p/bbce88/32.png) [@pumontjm](https://discuss.tryton.org/u/pumontjm)\
**Post date:** [June 27, 2023, 1:59pm UTC](https://discuss.tryton.org/t/retrieve-rows-conditionally/5231/6 "2023-06-27T13:59:50Z")

</div>

Hello,  
I am resuming this project after a long break.

I tried different things but it doesn’t work.

last test:

```auto
    <record model="ir.rule.group" id="rule_group_sequence_spending">
      <field name="name">spending list</field>
      <field name="model" search="[('model', '=', 'hr.employee.spending')]"/>
      <field name="global_p" eval="True"/>
    </record>
    <record model="ir.rule" id="rule_sequence_spending1">
      <field name="domain"
        eval="[('employee', 'in', Eval('employees', []))]"
        pyson="1"/>
      <field name="rule_group" ref="rule_group_sequence_spending"/>
    </record>
    <record model="ir.rule" id="rule_sequence_spending2">
      <field name="domain" eval="[('employee', '=', None)]" pyson="1"/>
      <field name="rule_group" ref="rule_group_sequence_spending"/>
    </record>

```

normal that it doesn’t work because I’m just replacing things without knowing what I’m doing.

I confirm that the user is indeed associated with an employee, that my list contains entries concerning several employees

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![udono](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/udono/32/2114_2.png) [@udono](https://discuss.tryton.org/u/udono)\
**Post date:** [June 27, 2023, 2:54pm UTC](https://discuss.tryton.org/t/retrieve-rows-conditionally/5231/7 "2023-06-27T14:54:44Z")

</div>

Hi,  
your intent looks similar the _own hours_ in the timesheet module which is defined here:

> **[modules/timesheet/line.xml · branch/default · Tryton / Tryton · GitLab](https://foss.heptapod.net/tryton/tryton/-/blob/branch/default/modules/timesheet/line.xml#L149)**
>
> Business Software Modularity, scalability & security for your business Documentation Contribute

[The difference is:](https://docs.tryton.org/projects/server/en/latest/topics/access_rights.html#record-rule)

> [@pumontjm](#):
>
> ` <field name="global_p" eval="True"/>`

` <field name="global_p" eval="False"/>`  
` <field name="default_p" eval="True"/>`

BTW also you can fiddle it out in the Tryton client UI like here:

[https://demo6.8.tryton.org/#demo6.8/model/ir.rule.group/60;name="Administration%20%2F%20Models%20%2F%20Record%20Rules"&views=[84%2C85]](https://demo6.8.tryton.org/#demo6.8/model/ir.rule.group/60;name=%22Administration%20%2F%20Models%20%2F%20Record%20Rules%22&views=%5B84%2C85%5D)

When it works, you can move the content to the XML.  
Also don’t forget to re-login into Tryton once you change the permissions.

---

<div class="post-metadata">

**Author:** ![pumontjm](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/p/bbce88/32.png) [@pumontjm](https://discuss.tryton.org/u/pumontjm)\
**Post date:** [June 28, 2023, 1:12pm UTC](https://discuss.tryton.org/t/retrieve-rows-conditionally/5231/8 "2023-06-28T13:12:44Z")

</div>

Thank you for your help. It helped me to continue the tests.  
But it still doesn’t work.

I did a test with a default value which worked:

```auto
    <record model="ir.rule.group" id="rule_group_sequence_spending">
      <field name="name">Dépenses employé</field>
      <field name="model" search="[('model', '=', 'hr.employee.spending')]"/>
      <field name="global_p" eval="True"/>
    </record>
    <record model="ir.rule" id="rule_sequence_spending1">
      <field name="domain" eval="[('employee', '=', '2')]" pyson="1"/>
      <field name="rule_group" ref="rule_group_sequence_spending"/>
    </record>

```

I conclude that the problem comes from here:

```auto
eval="[('employee', 'in', Eval('employees', []))]"

```

---

<div class="post-metadata">

**Author:** ![udono](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/udono/32/2114_2.png) [@udono](https://discuss.tryton.org/u/udono)\
**Post date:** [June 28, 2023, 1:22pm UTC](https://discuss.tryton.org/t/retrieve-rows-conditionally/5231/9 "2023-06-28T13:22:16Z")

</div>

> [@pumontjm](#):
>
> I conclude that the problem comes from here:
> 
> ```auto
> eval="[('employee', 'in', Eval('employees', []))]"
> 
> ```

so in the python class which defines the model `hr.employee.spending` you need to handle `employee` and `employees`. So maybe `employees` is always empty or filled with wrong values?

---

<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:** [June 28, 2023, 1:49pm UTC](https://discuss.tryton.org/t/retrieve-rows-conditionally/5231/10 "2023-06-28T13:49:34Z")

</div>

If it is working with `'2'` as value, it means that the employee field is not a `Many2One` but a `Char`.  
So the `Eval('employees', [])` is filled by the `company` with a list of ids not char.

> [@udono](#):
>
> so in the python class which defines the model `hr.employee.spending` you need to handle `employee` and `employees`. So maybe `employees` is always empty or filled with wrong values?

No `employees` is filled by the `company` module because rule evaluation is done with `ir.rule` context.

---

<div class="post-metadata">

**Author:** ![pumontjm](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/p/bbce88/32.png) [@pumontjm](https://discuss.tryton.org/u/pumontjm)\
**Post date:** [June 28, 2023, 2:08pm UTC](https://discuss.tryton.org/t/retrieve-rows-conditionally/5231/11 "2023-06-28T14:08:49Z")

</div>

> [@ced](#):
>
> If it is working with `'2'` as value

My bad. It’s work with the integer 2.

---

<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:** [June 28, 2023, 2:14pm UTC](https://discuss.tryton.org/t/retrieve-rows-conditionally/5231/12 "2023-06-28T14:14:52Z")

</div>

So your user has no employees set.

---

<div class="post-metadata">

**Author:** ![pumontjm](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/p/bbce88/32.png) [@pumontjm](https://discuss.tryton.org/u/pumontjm)\
**Post date:** [June 28, 2023, 2:24pm UTC](https://discuss.tryton.org/t/retrieve-rows-conditionally/5231/13 "2023-06-28T14:24:56Z")

</div>

Here are the employee fields of the two users I tested:

```auto
tryton64_rct=# select login, employee from res_user where login='jmeuh' or login='admin';
 login | employee 
-------+----------
 jmeuh | 2
 admin | 2

```

---

<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:** [June 28, 2023, 2:40pm UTC](https://discuss.tryton.org/t/retrieve-rows-conditionally/5231/14 "2023-06-28T14:40:44Z")

</div>

But it is the employees that is used and the behavior may depend on the value of company filter.

Be sure also that the record of the rule is correctly updated in the database.

---

<div class="post-metadata">

**Author:** ![pumontjm](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/p/bbce88/32.png) [@pumontjm](https://discuss.tryton.org/u/pumontjm)\
**Post date:** [June 29, 2023, 10:13am UTC](https://discuss.tryton.org/t/retrieve-rows-conditionally/5231/15 "2023-06-29T10:13:05Z")

</div>

> [@ced](#):
>
> But it is the employees that is used and the behavior may depend on the value of company filter.

sorry I don’t understand.

> [@ced](#):
>
> Be sure also that the record of the rule is correctly updated in the database.

I’m sure
