İçereği Atla
Menü
Bu soru işaretlendi
1 Cevapla
8460 Görünümler

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

Avatar
Vazgeç
Üretici

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?

Üretici En İyi Yanıt

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

Avatar
Vazgeç
İlgili Gönderiler Cevaplar Görünümler Aktivite
2
Mar 15
15048
0
Eyl 23
1613
1
Nis 24
1784
2
Ara 23
12879
2
Tem 19
10224