Odoo Help

Welcome!

This community 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.

0

Set default value for a many2one field in Odoo9

By
Dominic
on 5/28/16, 11:32 AM 1,189 views

Hi, 

When I check in google for that topic I get only results for odoo8 and the solution given for it does not work for me. So my questions is how I can set a default value of a many2one field in odoo?

My following example works fine, except that I did not find out how to set a default value:

my example code:

py file:

var = fields.Many2one('myNewModule', "My Text")


view:

<field name="myNewModule" domain="[('type', '=', 'other')]" />


Any hints?

2

Prakash Sharma

--Prakash Sharma--
815
| 2 1 4
Delhi, India
--Prakash Sharma--

I like to surround myself with brave, creative people.

My motivation for learning different technologies and algos drive me. I love challenges of a steep learning curve which I believe makes me a better learner. I have a strong desire to learn different technologies, which would enable me to become a full stack developer.

Prakash Sharma
On 5/30/16, 2:40 AM

Use lambda  like  default=lambda self: self.env['model.name'].search([]))  :

    journal_ids = fields.Many2many('account.journal', string='Journals', required=True, default=lambda self: self.env['account.journal'].search([]))

or function  Like  default=_default_tax_group

    @api.model
def _default_tax_group(self):
return self.env['account.tax.group'].search([], limit=1)
    tax_group_id = fields.Many2one('account.tax.group', string="Tax Group", default=_default_tax_group, required=True)


0
Dominic
On 6/5/16, 11:43 AM

Hello at all, 

Thanks for your answer. Actually your solution works, BUT not for this special case: 

I want to modify the "Country" Field in the Partner Module. So actually what I want is to have a special Country as default when a user wants to create a new contact. 

Your mentioned solution works for the almost all fields, BUT not for country field. Any ideas why the country field is so special in that case?

so I tried this: 

_defaults = {
'country_id': lambda self, cr, uid, context: self.pool.get('res.country').browse(cr, uid, self.pool.get('res.country').search(cr, uid, [('code','=','CO')]))[0].id,
'tdoc' : '13',
}


Unfortunately this solution works only for the tdoc field, but not for the country :-( 

At least not in Odoo9


Any ideas?

0

Pawan

--Pawan--
1267
| 4 3 5
Hyderabad, India
--Pawan--


Pawan
On 5/30/16, 2:26 AM

Dominic,

You can use this as your answer..

This will set current user to your new record...

user_id = fields.Many2one('res.users','User', default=lambda self: self.env.user)

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 5/28/16, 11:32 AM
Seen: 1189 times
Last updated: 6/5/16, 11:43 AM