Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
3 Trả lời
40821 Lượt xem

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?

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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)


Ảnh đại diện
Huỷ bỏ

thanks! it really helps me. :)

Tác giả Câu trả lời hay nhất

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?

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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)
Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 9 22
60
1
thg 3 15
9536
2
thg 2 25
5851
1
thg 12 24
1410
2
thg 1 23
2436