콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
1 회신
8472 화면

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
15061
0
9월 23
1615
1
4월 24
1784
2
12월 23
12879
2
7월 19
10239