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

Hello All,

I want to set default country "Hong Kong" for country_id field. So what function i write for that in odoo10. Now my python code is below:

py code:

@api.depends('country_id')
def _get_default_country(self, context=None):
if self.country_id:
return {'domain': [('country_id', '=', self.country_id.id)]}
print "yes:", 235
return True

_defaults = {

'country_id': _get_default_country
}
country_info = fields.Char(compute='_get_default_country', string='Default Country')
Ảnh đại diện
Huỷ bỏ

For setting default value for a field from the user interface: https://www.youtube.com/watch?v=cQYHiGAd9k4

Câu trả lời hay nhất

Hello Pawan Sharma,

You are using odoo v10 as per that I suggest below code, I hope this will help you.
See below code :

    @api.model

    def _get_default_country(self):

        country = self.env['res.country'].search([('code', '=', 'HK')], limit=1)

        return country


    country_id = fields.Many2one('res.country', string='Country',

                                 default=_get_default_country)

If you want to see all Countries and its code then go to Sales => Configuration => Contacts => Localization => Countries.

Thanks

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

Thanks it's working....!!

Ảnh đại diện
Huỷ bỏ
Tác giả

i'm also applied by id. Add it's also working.

My python code is below:

(Here 95 is Hong Kong Id)

@api.model

def _get_default_country(self):

print "yes:", 95

return 95

country_id = fields.Many2one('res.country', "Country", change_default=True, default=_get_default_country)

Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 1 18
2884
2
thg 3 24
11271
5
thg 9 20
5783
1
thg 12 19
9119
0
thg 7 19
3720