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,242 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



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
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: 1242 times
Last updated: 8/31/15, 10:30 AM