设置默认国家

Set Default Country

我想为 country_id 字段设置默认国家/地区 "Hong Kong"。那么我在 odoo10 中为此编写了什么功能。现在我的 python 代码如下:

py代码:

@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')

Return 值始终与字段数据类型匹配。在您的情况下,您正在存储 "char" 值和 return "Boolean" (true/false)。那是没有意义的。

如果你想下拉列表然后改变 "country_info" 数据类型从 "Char" 到 "Many2one" 你的默认函数应该 return "integer"根据您的逻辑值。

Python Lambda 函数主要与函数 filter()、map() 和 reduce() 结合使用。

ODOO 10

Python代码:

country_id = fields.Many2one('res.country', string='Country', default=lambda self: self.env['res.country'].browse([(95)]))

XML代码:

<field name="country_id" readonly="1"/>