Odoo17 技巧 | 如何获取Selection字段的显示值五种方法
在 Odoo 中,要获取 fields.Selection
字段的汉字内容(即显示值),可以通过以下方法实现:
方法 1:在模型内部使用(服务端)
# 在模型方法中获取当前记录的 selection 字段显示值 def get_selection_label(self): # 获取字段的 selection 列表 selection_dict = dict(self._fields['rqzt'].selection) # 获取当前记录的字段值对应的标签 return selection_dict.get(self.rqzt, '')
方法 2:在外部模型或脚本中使用
# 获取任意模型的 selection 字段显示值 def get_selection_label(model_name, field_name, value): model = request.env[model_name] # 或 env[model_name] field = model._fields[field_name] selection_list = field.selection # 处理动态 selection(如果是函数) if callable(selection_list): selection_list = selection_list(model) selection_dict = dict(selection_list) return selec