跳至内容
菜单
此问题已终结
1 回复
8465 查看

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>
```

形象
丢弃
相关帖文 回复 查看 活动
2
3月 15
15056
0
9月 23
1615
1
4月 24
1784
2
12月 23
12879
2
7月 19
10230