# Domain Issue in the Module

**URL:** <https://discuss.tryton.org/t/domain-issue-in-the-module/7430>\
**Category:** Developer\
**Created:** [July 5, 2024, 1:15pm UTC](https://discuss.tryton.org/t/domain-issue-in-the-module/7430 "2024-07-05T13:15:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mimo\_Alva](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@mimo\_Alva](https://discuss.tryton.org/u/mimo_Alva)\
**Post date:** [July 5, 2024, 1:15pm UTC](https://discuss.tryton.org/t/domain-issue-in-the-module/7430/1 "2024-07-05T13:15:56Z")

</div>

Hi for all;

I need some help. I want to ensure that if the user is not named ‘Marcos’ or ‘admin’, the records in the party will only show the records where the party name is equal to the name of the logged-in user else when user.name are ‘Marcos’ or ‘admin’ , all the records will be visible on party module.

Here is the Xml code that i have add

```auto
            <field name="domain" eval="(
                ('name', '=', Eval('_user', {}).get('name')) if Eval('_user', {}).get('name') not in ('Marcos', 'admin') else ()
            )" pyson="1"/>

```

Here is the completed code

```auto
        <record model="ir.ui.view" id="party_view_tree">
            <field name="model">party.party</field>
            <field name="type">tree</field>
            <field name="name">party_tree</field>
        </record>

        <record model="ir.ui.view" id="party_view_form">
            <field name="model">party.party</field>
            <field name="type">form</field>
            <field name="name">party_form</field>
        </record>

        <record model="ir.action.act_window" id="act_party_form">
            <field name="name">Parties</field>
            <field name="res_model">party.party</field>
            <field name="domain" eval="(
                ('name', '=', Eval('_user', {}).get('name')) if Eval('_user', {}).get('name') not in ('Marcos', 'admin') else ()
            )" pyson="1"/>        
        </record>

        <record model="ir.action.act_window.view" id="act_party_form_view1">
            <field name="sequence" eval="10"/>
            <field name="view" ref="party_view_tree"/>
            <field name="act_window" ref="act_party_form"/>
        </record>

        <record model="ir.action.act_window.view" id="act_party_form_view2">
            <field name="sequence" eval="20"/>
            <field name="view" ref="party_view_form"/>
            <field name="act_window" ref="act_party_form"/>
        </record>

```

Now when i test the code and logged in with any user.name no records i find in Party Module, any help will be appreciated. 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:** [July 5, 2024, 1:43pm UTC](https://discuss.tryton.org/t/domain-issue-in-the-module/7430/2 "2024-07-05T13:43:14Z")

</div>

You can not use Python `if` is a PYSON statement, you must use the [`If()`](https://docs.tryton.org/latest/server/ref/pyson.html#if) of PYSON.  
The action domain is evaluated by the client, but the `_user` keyword is just the user `id`. If you want to evaluate against more data, you need to put them in the user context so they will be available as `Eval('context', {}).get(...)`

Also if you are looking for access control, you should use [record rule](https://docs.tryton.org/latest/server/topics/access_rights.html#record-rule) but since [Remove user from evaluation context of rule domain (52ba840937ba) · Commits · Tryton / Tryton · GitLab](https://code.tryton.org/tryton/commit/52ba840937ba) the user is no more in the evaluation context. So you will have to add it.  
But adding such restriction on a so common model than party could be very challenging because it is used as reference in many other models.

---

<div class="post-metadata">

**Author:** ![mimo\_Alva](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@mimo\_Alva](https://discuss.tryton.org/u/mimo_Alva)\
**Post date:** [July 5, 2024, 2:09pm UTC](https://discuss.tryton.org/t/domain-issue-in-the-module/7430/3 "2024-07-05T14:09:37Z")

</div>

Thanks i have tried this,

> [@ced](#):
>
> You can not use Python `if` is a PYSON statement, you must use the [`If()`](https://docs.tryton.org/latest/server/ref/pyson.html#if) of PYSON.  
> The action domain is evaluated by the client, but the `_user` keyword is just the user `id`. If you want to evaluate against more data, you need to put them in the user context so they will be available as `Eval('context', {}).get(...)`

I have tried that

```auto
<field name="domain" eval="If(
        Eval('context', {}).get('user', {}).get('name') not in ['Marcos', 'admin'],
        [('name', '=', Eval('context', {}).get('user', {}).get('name'))],
        []
    )" pyson="1"/>

```

—\> but still facing same issue no records no matter the user.name logged in

---

<div class="post-metadata">

**Author:** ![mimo\_Alva](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@mimo\_Alva](https://discuss.tryton.org/u/mimo_Alva)\
**Post date:** [July 5, 2024, 2:10pm UTC](https://discuss.tryton.org/t/domain-issue-in-the-module/7430/4 "2024-07-05T14:10:48Z")

</div>

> [@ced](#):
>
> Also if you are looking for access control, you should use [record rule](https://docs.tryton.org/latest/server/topics/access_rights.html#record-rule) but since [Remove user from evaluation context of rule domain (52ba840937ba) · Commits · Tryton / Tryton · GitLab](https://code.tryton.org/tryton/commit/52ba840937ba) the user is no more in the evaluation context. So you will have to add it.

I didn’t get well the idea must i also use record rule ?? For me, I am looking just for the visibility control of parties records based on the user.name

---

<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:** [July 5, 2024, 2:13pm UTC](https://discuss.tryton.org/t/domain-issue-in-the-module/7430/5 "2024-07-05T14:13:32Z")

</div>

`user` is not in the context and even less as a dictionary.  
You must fill yourself the context for example by extending `res.user` and add `name` to `_context_fields`.

---

<div class="post-metadata">

**Author:** ![mimo\_Alva](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@mimo\_Alva](https://discuss.tryton.org/u/mimo_Alva)\
**Post date:** [July 5, 2024, 3:07pm UTC](https://discuss.tryton.org/t/domain-issue-in-the-module/7430/6 "2024-07-05T15:07:53Z")

</div>

> [@mimo\_Alva](#):
>
> ```auto
> <field name="domain" eval="If(
> Eval('context', {}).get('user', {}).get('name') not in ['Marcos', 'admin'],
> [('name', '=', Eval('context', {}).get('user', {}).get('name'))],
> []
> )" pyson="1"/>
> 
> ```

thanks for your rapid response again, I have tried that now, Here is the steps that i have did now , i have add name in res.user model like that:

```
    cls._context_fields = [
        'name',
        'language',
        'language_direction',
        'groups',
    ]

```

then i have updated my Xml code like that

```auto
<record model="ir.action.act_window" id="act_party_form">
    <field name="name">Parties</field>
    <field name="res_model">party.party</field>
    <field name="domain" eval="If(
            Eval('context', {}).get('user', {}).get('name') not in ['Marcos', 'admin'],
            [('name', '=', Eval('context', {}).get('user', {}).get('name'))],
            []
        )" pyson="1"/>
</record>

<record model="ir.model.access" id="access_party_party_user">
    <field name="model" search="[('model', '=', 'party.party')]"/>
    <field name="perm_read" eval="1"/>
    <field name="perm_write" eval="1"/>
    <field name="perm_create" eval="1"/>
    <field name="perm_delete" eval="1"/>
</record>

        <record model="ir.rule.group" id="group_party_user_access">
            <field name="name">Party User Access Rule</field>
            <field name="model" search="[('model', '=', 'party.party')]"/>
            <field name="global_p" eval="True"/>
        </record>

        <record model="ir.rule" id="rule_party_user_access">
         <field name="domain" eval="If(
            Eval('context', {}).get('user', {}).get('name') not in ['Marcos', 'admin'],
            [('name', '=', Eval('context', {}).get('user', {}).get('name'))],
            []
        )" pyson="1"/>
    <field name="rule_group" ref="group_party_user_access"/>
</record>

```

—\> But i still faced same issue, no matter the user.name logged in , no records shown in Party module.

---

<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:** [July 5, 2024, 4:03pm UTC](https://discuss.tryton.org/t/domain-issue-in-the-module/7430/7 "2024-07-05T16:03:11Z")

</div>

> [@mimo\_Alva](#):
>
> `Eval('context', {}).get('user', {}).get('name')`

The context fields are directly in the context so it should be:

`Eval('context', {}).get('name')`

> [@mimo\_Alva](#):
>
> `Eval('context', {}).get('user', {}).get('name') not in ['Marcos', 'admin']`

You can neither user Python operator `not in` in PYSON as it is evaluated directly. You must use the PYSON [`In()`](https://docs.tryton.org/latest/server/ref/pyson.html#in) and [`Not()`](https://docs.tryton.org/latest/server/ref/pyson.html#not).

So your domain should look like:

```python
If(~Eval('context', {}).get('name').in_(['Marcos', 'admin']), [('name', '=', Eval('context', {}).get('name'))], [])

```

I see that you put also an access rule, but as I said this is going to be very difficult to implement because you need to enforce the rule to all models that use party as reference.  
Any way in record rule, you must add the user to the `Rule._get_context` in order to have `user` in the evaluation context. And you must also add the user to the `Rule._get_cache_key`.  
Then the domain should look like `[('name', '=', Eval('user', {}).get('name'))]`.

---

<div class="post-metadata">

**Author:** ![mimo\_Alva](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@mimo\_Alva](https://discuss.tryton.org/u/mimo_Alva)\
**Post date:** [July 5, 2024, 5:14pm UTC](https://discuss.tryton.org/t/domain-issue-in-the-module/7430/8 "2024-07-05T17:14:19Z")

</div>

Okay now i have deleted the access rule, I thought it’s obligatory to do it to control visibility? so i need to confirm is it obligatory because i deleted it ? and i tested with your domain suggestion

```auto
<record model="ir.action.act_window" id="act_party_form">
    <field name="name">Parties</field>
    <field name="res_model">party.party</field>
    <field name="domain" eval="If(~Eval('context', {}).get('name').in_(['Marcos', 'admin']), [('name', '=', Eval('context', {}).get('name'))], [])" pyson="1"/>
</record>

```

—\>but nothing happen when i try to type on Parties menu

Thanks again Mr.@ced.

---

<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:** [July 5, 2024, 5:18pm UTC](https://discuss.tryton.org/t/domain-issue-in-the-module/7430/9 "2024-07-05T17:18:00Z")

</div>

> [@mimo\_Alva](#):
>
> Okay now i have deleted the access rule, I thought it’s obligatory to do it to control visibility? so i need to confirm is it obligatory because i deleted it ?

The access rule is required if you really forbid any user to see any other parties. But as I said this will make Tryton mostly unusable because almost every model is linked to a party in some way.

> [@mimo\_Alva](#):
>
> but nothing happen when i try to type on Parties menu

Is the `name` in the user context? Did you re-login?
