Odoo Help


How to pass an XML_ID in field default value through context ?

Henri-Maxime Ducoulombier
on 11/13/14, 11:53 AM 6,030 views


I have a field, say "source_ids" in a form. These items (the sources) have a type, identified with a many2one relation as the type_id field.

In my view, I want to set the default type to an XML_ID (and not the integer ID of the record).

So for instance, my type "FOO" has ID 10, and XML_ID : "ref_module_id.source_type_id".

When I do that, I have an error (it expects an integer) :

<field name="source_id" context="{'default_type_id':'reference_module_id.source_type_id'}"/>

Can anybody help ? I'd really like to avoid setting the numeric value in my view XML...




can you just try like below ?

<field name="source_id" context="{'default_type_id':ref('reference_module_id.source_type_id')}"/>

I hope it will resolve your issue.

Hi, I did try that before, but it raises this error : Uncaught Error: NameError: name 'ref' is not defined

Henri-Maxime Ducoulombier
on 11/14/14, 2:31 AM
Marvin Taboada
On 9/10/15, 12:50 AM

Hello Henri-Maxime, I'm afraid you can't use `ref()` inside context XML attributes.

You can however define the context in actions and use `ref()` combined with the `eval` XML attribute, as you can see in the following link, where it is used to define default values for fields in the form: https://github.com/odoo/odoo/blob/8.0/addons/account/account_bank_view.xml#L42

By default, actions are not directly related to particular views (view `priority` is used to select a particular view to render), so you may think this approach has the limitation of not affecting any particular views.

You can, however, explicitly associate actions with particular Form/Tree views by creating additional `ir.actions.act_window.view` records. Please see the holidays module example where the same object `hr.holidays` is used to represent both leaves and allocations, having differentiated actions/forms/trees at the same time, as you can guess, leaves and allocations have different actions/contexts/forms/trees: https://github.com/odoo/odoo/blob/8.0/addons/hr_holidays/hr_holidays_view.xml#L235

I hope this helps.


About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

2 follower(s)


Asked: 11/13/14, 11:53 AM
Seen: 6030 times
Last updated: 3/27/16, 7:13 AM