# Getting key value from user preferences in domain?

**URL:** <https://discuss.tryton.org/t/getting-key-value-from-user-preferences-in-domain/3698>\
**Category:** Developer\
**Created:** [January 17, 2021, 8:15pm UTC](https://discuss.tryton.org/t/getting-key-value-from-user-preferences-in-domain/3698 "2021-01-17T20:15:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![deathrake](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@deathrake](https://discuss.tryton.org/u/deathrake)\
**Post date:** [January 17, 2021, 8:15pm UTC](https://discuss.tryton.org/t/getting-key-value-from-user-preferences-in-domain/3698/1 "2021-01-17T20:15:13Z")

</div>

Hey i just added a new element in context with the following code

```
    @classmethod
    def _get_preferences(cls, user, context_only=False):
        res = super(User, cls)._get_preferences(user,
            context_only=context_only)
        if user.employee:
                res['party_id'] = user.employee.party.id
        print(res)
        return res

```

the party\_id is added to the dictionary but when i want to fetch the party\_id using domain  
`[['party.id','=',Eval('context').get('party_id')]]`

this doesnt seems to work

not sure what i am doing wrong here any help is greatly appreciated

---

<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:** [January 18, 2021, 7:19pm UTC](https://discuss.tryton.org/t/getting-key-value-from-user-preferences-in-domain/3698/2 "2021-01-18T19:19:42Z")

</div>

> [@deathrake](#):
>
> this doesnt seems to work

Could you explain what is not working? What you have coded and what you are trying to achieve.

---

<div class="post-metadata">

**Author:** ![deathrake](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@deathrake](https://discuss.tryton.org/u/deathrake)\
**Post date:** [January 19, 2021, 5:43am UTC](https://discuss.tryton.org/t/getting-key-value-from-user-preferences-in-domain/3698/3 "2021-01-19T05:43:47Z")

</div>

> [@ced](#):
>
> Could you explain what is not working? What you have coded and what you are trying to achieve.

I wrote this code in `module/company/res.py`

> [@deathrake](#):
>
> ```auto
> @classmethod
> def _get_preferences(cls, user, context_only=False):
> res = super(User, cls)._get_preferences(user,
> context_only=context_only)
> if user.employee:
> res['party_id'] = user.employee.party.id
> print(res)
> return res
> 
> ```

`print(res)` output the following

`{'name': 'john', 'password': 'xxxxxxxxxx', 'email': '', 'signature': '', 'menu': 2, 'menu.rec_name': 'Menu', 'pyson_menu': '{"res_model": "ir.ui.menu", "context_model": null, "context_domain": null, "id": 2, "limit": null, "name": "Menu", "type": "ir.action.act_window", "pyson_domain": "[[\\"parent\\", \\"=\\", null]]", "pyson_context": "{}", "pyson_order": "[[\\"sequence\\", \\"ASC NULLS FIRST\\"], [\\"id\\", null]]", "pyson_search_value": "[]", "domains": [], "views": [[3, "tree"]], "icon.": null}', 'actions': [], 'status_bar': 'john [rs]', 'warnings': [], 'applications': [], 'mobile': '', 'dashboard_layout': 'square', 'dashboard_actions': [], 'employee': 1, 'employee.rec_name': 'john', 'company': 1, 'company.rec_name': 'Test company', 'language': 'en', 'language_direction': 'ltr', 'groups': [4], 'roles': [], 'party_id': 1, 'main_company': 1, 'main_company.rec_name': 'Test company', 'employees': [1]}`

but when i try to get the **`party_id`** in the following manner in record rule

 ![image](https://discuss-cdn.tryton.org/uploads/default/original/2X/9/9a330b53c3093366bc43a2f9da774fd9ce787f56.png)

seems like the domain does not work to fetch data based on party id

---

<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:** [January 19, 2021, 9:40am UTC](https://discuss.tryton.org/t/getting-key-value-from-user-preferences-in-domain/3698/4 "2021-01-19T09:40:16Z")

</div>

So I guess you are trying to write a record rule. You can not use the context for record rule because it is not safe. Any user can fill the context with any value.  
If you need a specific value available in the evaluation context of record rule, you must override `Rule._get_context`.

---

<div class="post-metadata">

**Author:** ![deathrake](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@deathrake](https://discuss.tryton.org/u/deathrake)\
**Post date:** [January 19, 2021, 5:24pm UTC](https://discuss.tryton.org/t/getting-key-value-from-user-preferences-in-domain/3698/5 "2021-01-19T17:24:37Z")

</div>

```
class Rule(metaclass=PoolMeta):
     __name__ = 'ir.rule'

   @classmethod
   def _get_context(cls):
        pool = Pool()
        User = pool.get('res.user')
        Party = pool.get('party.party')
        context = super()._get_context()

        with Transaction().set_user(0):
            user = User(Transaction().user)

        if user.employee:
           context['party_id'] = EvalEnvironment(
                    Party(user.employee.party.id), Party)

        return context

```

i wrote this code but still no luck am i doing something wrong ?  
an example would be greatly appreciated

---

<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:** [January 19, 2021, 5:46pm UTC](https://discuss.tryton.org/t/getting-key-value-from-user-preferences-in-domain/3698/6 "2021-01-19T17:46:02Z")

</div>

So your rule should be written as: `[('party.id', '=', Eval('party_id', {}).get('id'))]`

_PS: It is strange to suffix an instance name with `_id`_

---

<div class="post-metadata">

**Author:** ![deathrake](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@deathrake](https://discuss.tryton.org/u/deathrake)\
**Post date:** [January 20, 2021, 2:39pm UTC](https://discuss.tryton.org/t/getting-key-value-from-user-preferences-in-domain/3698/7 "2021-01-20T14:39:44Z")

</div>

that was life saving thanks

---

<div class="post-metadata">

**Author:** ![system](https://discuss-cdn.tryton.org/uploads/default/original/1X/c6f8ec0a40525cdcd50058c734283450a4b3d38b.png) [@system](https://discuss.tryton.org/u/system)\
**Post date:** [February 19, 2021, 2:39pm UTC](https://discuss.tryton.org/t/getting-key-value-from-user-preferences-in-domain/3698/8 "2021-02-19T14:39:46Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
