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,691 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...



Emipro Technologies Pvt. Ltd.

--Emipro Technologies Pvt. Ltd.--
| 6 7 8
Rajkot, India
--Emipro Technologies Pvt. Ltd.--

Being an emerging leader in IT market since 2011, Emipro Technologies Pvt. Ltd. has been providing a wide range of business solutions in Odoo & Magento. We are pleased to have a large pool of contented customers with our meticulous work in the domain of ERP & e-Commerce. A ray of relief & satisfaction to  our customers heart by our successful deployment in their organization, purely represents our success in Odoo platform. Hence, we take pride for being an Odoo partner with a vision of expanding our strategic alliances with our customers to offer them high value-added, trustworthy & cost effective solutions.


Since establishment, our business has span across 11 countries of the world. Our customers are companies of all sizes ranging from start-ups to large enterprises, who realize that they need a professional business solutions to generate revenue streams, establish proper communication channels or streamline business operations. Standing with strong determination of customer satisfaction, observing each minute detail of their business processes, providing proper guidance and moving forth to develop product accordingly adds our value and reliability among our customers. 


We are blessed with efficient, passionate & eclectic young developers who have come across almost all kinds of business profiles, working with full dedication, applying creativity & new features in existing modules and completing customer's projects successfully on predefined target. Our in-depth knowledge while giving business solutions in Odoo allows us to offer following services to our customers :


  • Consulting
  • Installation, Configuration & Customization
  • Training & Support
  • Maintenance
  • Upgradation
  • Documentation
  • Crafting community modules
  • Drafting videos on demand
  • Smart Apps development

We will be :) to welcome you @ www.emiprotechnologies.com to solve any kind of your business needs around Odoo. However we are just an inch away from you by email info@emiprotechnologies.com or a tweet to @EmiproTech

Emipro Technologies Pvt. Ltd.
On 11/14/14, 12:15 AM


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: 6691 times
Last updated: 3/27/16, 7:13 AM