Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
1 ตอบกลับ
8463 มุมมอง

In `xml` I want to use both `active_id` and `ref` as `context` field's value at once while defining `record` for model `ir.actions.act_window`. But I am able to use only one as shown in below code snippets:

Method 1. Using only `ref` with `eval`:
```xml
<record id="some_identifier" model="ir.actions.act_window">
<field name="name">Name</field>
...
<field name="context" eval="{'some_key': ref('module_name.ext_id')}" />
</record>
```

Method 2. Using only `active_id`:
```xml
<record id="some_identifier" model="ir.actions.act_window">
<field name="name">Name</field>
...
<field name="context">{'search_default_employee_id': active_id}</field>
</record>
```

When I try using `active_id` in *Method 1.* or include `ref` in *Method 2.* above I get an error message saying **`active_id` is not defined** in first case and **`ref` is not defined** in second case.

What I would like to achieve is to be able to use both `ref` method and `active_id` variable at once. Something like below:

```xml
<record id="some_identifier" model="ir.actions.act_window">
<field name="name">Name</field>
...xml
<field name="context" eval="{'some_key': ref('module_name.ext_id'), 'search_default_employee_id': active_id}" /> <!-- NOTE: Raises an exception -->
</record>
```

```xml
<record id="some_identifier" model="ir.actions.act_window">
<field name="name">Name</field>
...
<field name="context">{'some_key': ref('module_name.ext_id'), 'search_default_employee_id': active_id}</field> <!-- NOTE: Raises an exception -->
</record>
```

อวตาร
ละทิ้ง
ผู้เขียน

I solved this myself. See my answer below or at Stackoverflow => https://stackoverflow.com/q/59545188/2071612

that's great. but what was the traceback and why eval could not work for both at the same time?

ผู้เขียน คำตอบที่ดีที่สุด

Starting from v12 Odoo supports calling model method for evaluating field value. We can utilize this technique to use both `ref` method and `active_id` variable at once in `context` field's value in following way.

Define a method as follow in any `model` you want.

```python
from odoo import models, api
from odoo.tools.misc import unquote

class SomeClass(models.Model):
_inherit = 'some_module.model.name'

@api.model
def _using_both_ref_and_active_id(self):
ref = self.env.ref
active_id = unquote("active_id")

return {
'using_ref': ref('external.identifier').id,
'search_default_whatever_filter_name': [active_id],
'default_whatever_field_name': active_id
}
```

And then in `xml` access this `method` as follows:

```xml
<record id="action_view_som_id" model="ir.actions.act_window">
<field name="name">Files</field>
...
<field name="context" model="some_module.model.name" eval="obj()._using_both_ref_and_active_id()" />
</record>
```

อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
How to pass active id to a popup แก้ไขแล้ว
2
มี.ค. 15
15053
0
ก.ย. 23
1615
1
เม.ย. 24
1784
2
ธ.ค. 23
12879
How to pass a context value to a filter แก้ไขแล้ว
2
ก.ค. 19
10229