Odoo Help


How to fill in the default value by view, rather than models? [Closed]

Gavin Yap
on 7/22/15, 1:57 AM 1,677 views

I've extended  the partner model as below.

from openerp import models, fields, api, exceptions
class Partner(models.Model):
_name = 'res.partner'
_inherit = 'res.partner'
def _get_partner_category(self):
id = self.env['res.partner.category'].search([['name', '=', 'Registrant']]).id
return [(6,0,[id])]
national_id = fields.Char(string="National ID")
_defaults = {
'category_id': _get_partner_category,

As above, the code has made category_id and set the default tag to "Registrant". 

However, this affect other modules that uses the res.partner model.

Can I set defaults on my views instead via the model?

Gavin Yap
On 7/23/15, 3:02 AM


Create a data xml that has a tag that I want.

        <record model="res.partner.category" id="registrant_tag">
<field name="name">Registrant</field>

Now on my window action reference the to the tag with an eval option.

 <record model="ir.actions.act_window" id="registrant_list_action">
<field name="name">Registrant</field>
<field name="res_model">res.partner</field>
<field name="domain">[('category_id.name','ilike','Registrant')]</field>
<field name="context" eval="{'default_category_id': [(6,0,[ref('warranty.registrant_tag')])] }"/>
<field name="view_type">form</field>
<field name="view_mode">kanban,tree,form</field>
<field name="help" type="html">
<p class="oe_view_nocontent_create">Add a registrant


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 7/22/15, 2:37 AM


You can't set default value in View wise.  However you can set default values in Window Action.

For example, 

1. Go to Window Action menu, find "Countries" action and open it.

2. In context field, set "{'default_name':'India'}" 

3. Go to Countries menu Sales >> Configuration >> Address Book >> Localization >> Countries and open it. Try to create new record. You will see name "India" by default.

Tip : In order to set default from front end, You need to add your exact field name followed by "default_" prefix in Window Action's context field.

 Hope this helps,

It works partially. In the context field, I set "{'default_category_id':'[(6,0,[18])]'}" where'18' is the "Registrant" index. how do I assigned a function value to it? I tried "{'default_category_id': _get_partner_category}" it doesnt work.

Gavin Yap
on 7/22/15, 3:23 AM

Thanks for the tip, I have found my solution. :)

Gavin Yap
on 7/23/15, 2:58 AM
--Ahmed M.Elmubarak--

Ahmed M.Elmubarak
On 7/22/15, 8:13 AM


Your function _get_partner_category , catch the context that comes from the action, So you can add some custom value to the action's context just like:

{'my_menu': 'my_custom_menu'}

Then in your function check if the context contains the value 'my_menu' if yes then return the calculated value ...


Sorry, but I still don't quite understand what you mean.? In Emipro's is already using an action and a context. Now you are suggesting to use another action ?

Gavin Yap
on 7/22/15, 11:11 AM

No, no another action. Empiro suggested to use the [ default_ ] in the action's context, to set the defaults value, but you can add a new value to the context. Then add some checks to your function. Just try to print the context in your function to get it . in your function just write: print self._context or self.env.context

Ahmed M.Elmubarak
on 7/23/15, 2:47 AM

Thanks Ahmed, I think I know where you are getting at but I think I'm quite satisfied with the tip Empiro provide me and manage to achieve what I want

Gavin Yap
on 7/23/15, 9:15 AM

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

3 follower(s)


Asked: 7/22/15, 1:57 AM
Seen: 1677 times
Last updated: 8/31/15, 10:30 AM