Skip to main content
 首页 » 编程设计

python之设置默认国家

2025年05月04日24mfrbuaa

我想为 country_id 字段设置默认国家“香港”。那么我在 odoo10 中为此编写了什么功能。现在我的 python 代码如下:

代码:

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

请您参考如下方法:

返回值始终与字段数据类型相匹配。在您的情况下,您正在存储“char”值并返回“Boolean”(真/假)。这是没有意义的。

如果您想要下拉列表,请将“country_info”数据类型从“Char”更改为“Many2one”,您的默认函数应根据您的逻辑返回“整数”值。