# Add Variable to Context

**URL:** <https://discuss.tryton.org/t/add-variable-to-context/8750>\
**Category:** Developer\
**Created:** [July 30, 2025, 11:36am UTC](https://discuss.tryton.org/t/add-variable-to-context/8750 "2025-07-30T11:36:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bromoapp](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bromoapp](https://discuss.tryton.org/u/bromoapp)\
**Post date:** [July 30, 2025, 11:36am UTC](https://discuss.tryton.org/t/add-variable-to-context/8750/1 "2025-07-30T11:36:57Z")

</div>

Hi,

I want to add a variable into context e.g. **‘employee\_id’ : ‘em0110220’**.  
So on other class I can access it as below:

```auto
context = Transaction().context
employee_id = context.get('employee_id')

```

But I couldn’t find example.

One more question:

I have a class named Employee and what I want is: when ever a user open the detail page that load Employee data from DB and displayed to the front end.. I want the class to set the context variable as above.

My question: how to trigger context variable creation when Employee loaded? any builtin classmethod which I need to implement to make this class triggers context variable creation on data load?

Regards  
Bromo

---

<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:** [July 30, 2025, 12:22pm UTC](https://discuss.tryton.org/t/add-variable-to-context/8750/2 "2025-07-30T12:22:41Z")

</div>

Normally the `employee` key is set on the context if the user has set a values in its own preferences.

You may have a look on the [company module source](https://code.tryton.org/tryton/-/blob/branch/default/modules/company/res.py) which is the one which adds the needed fields on the user model and sets the values on the context. This way the values are stored on the client customer and send back to the server on each request.

---

<div class="post-metadata">

**Author:** ![bromoapp](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bromoapp](https://discuss.tryton.org/u/bromoapp)\
**Post date:** [July 30, 2025, 12:29pm UTC](https://discuss.tryton.org/t/add-variable-to-context/8750/3 "2025-07-30T12:29:53Z")

</div>

Actually this is what I want todo @pokoli 🙂

```auto
        if cls.contains_key(domain, 'running_members.member.id') == True:
            seek_user_id = domain[1][2]
            logger.info(f">>>>>>>>> CONTAINS KEY running_members.member.id: {seek_user_id}")
            Transaction().set_context(employee_id = seek_user_id)

```

And from other class:

```auto
            employee_id = Transaction().context.get('employee_id')
            if employee_id == None:
                logger.info(">>>>>>>>>>>>>>>>>>>>>>>> EMPLOYEE ID NOT FOUND")

```

I can’t seem to add new variable in the context.

Bromo

---

<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:** [July 30, 2025, 12:47pm UTC](https://discuss.tryton.org/t/add-variable-to-context/8750/4 "2025-07-30T12:47:04Z")

</div>

I don’t know in which version you are working, but the later versions have several keys removed from the context, including employee to make caching work better. Also context should be treated as kind of unsecure because a user can change the context.

The question is what you want to achieve. Most of the time the employee is used to filter out records or check if the employee is in a group etc. If you want that kind of checks, look at for example at [modules/timesheet/ir.py · branch/default · Tryton / Tryton · GitLab](https://foss.heptapod.net/tryton/tryton/-/blob/branch/default/modules/timesheet/ir.py) to see how you can add the employee back into the context, but now on the server and not in the client.

P.S. the bots of the bullshitgenerators are scraping the internet on our costs by exhausting our bandwidth. To fight that several checks are in place which makes the names of the links disappear. The internet is not a fun place to be anymore 😢

---

<div class="post-metadata">

**Author:** ![bromoapp](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bromoapp](https://discuss.tryton.org/u/bromoapp)\
**Post date:** [July 30, 2025, 12:51pm UTC](https://discuss.tryton.org/t/add-variable-to-context/8750/5 "2025-07-30T12:51:46Z")

</div>

No this employee thing is part of our custom module.. totally not utilizing built-in Tryton or add-in Tryton feature.

I just want to inject a variable called employee\_id into context and retrieves and use it on another class. I just need to be able to set it in the context.

Bromo

---

<div class="post-metadata">

**Author:** ![dave](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dave](https://discuss.tryton.org/u/dave)\
**Post date:** [July 30, 2025, 2:30pm UTC](https://discuss.tryton.org/t/add-variable-to-context/8750/6 "2025-07-30T14:30:47Z")

</div>

I’m not entirely clear at what point you are trying to set the context value, and at what point you are using it, however…

Normally `set_context` is used as a [context manager](https://docs.python.org/3/reference/datamodel.html#context-managers) for example: [use of set\_context in the sale module](https://foss.heptapod.net/tryton/tryton/-/blob/fb2e9d4ead1a1279dca79b1fc3d1daefe9e2df90/modules/sale/sale.py#L569)  
Note, the context is only set for the duration of the `with` block, and `Model`’s objects from outside the `with` block may have have a different context to the one you are expecting as I think they get their context when they are instanciated (although you can re[browse](https://docs.python.org/3/reference/datamodel.html#context-managers) them to reinstanciate them with the current context).

Something else you may want to take a quick look at, if you want the context to be set for related records, is the [field’s context property](https://docs.tryton.org/latest/server/ref/fields.html#trytond.model.fields.Field.context), e.g.: [Use of context for a field in the sale module](https://foss.heptapod.net/tryton/tryton/-/blob/fb2e9d4ead1a1279dca79b1fc3d1daefe9e2df90/modules/sale/sale.py#L162)

Also it is possible to use a context model to set the context for a window (but I don’t think this is what you are looking for): e.g.: [use of context model in the sale module](https://foss.heptapod.net/tryton/tryton/-/blob/fb2e9d4ead1a1279dca79b1fc3d1daefe9e2df90/modules/sale/sale_reporting.xml#L48)
