一、简介
Selection
字段类型一般作为下拉列表显示,selection 的选项内容基本是固定,但是针对一些特殊要求,根据权限组显示不同的 selection 内容,也可以通过动态创建选项的内容,下面简单介绍Selection
的三种使用案例。
好文章 记得收藏+点赞+关注额 !!!
---- Nick.Peng
二、固定的 Selection 内容
- 如图,此种方式最常见
三、根据权限组显示不同的 selection 内容
-
指定属性
selection=’_selection_filter’
表示引用_selection_filter
函数进行不同权限组之间的内容显示。
-
_selection_filter
函数的定义。
@api.model def _selection_filter(self): """ 根据权限组显示不同的Selection值 """ res_filter = [ ('math', '数学'), ('chinese', '语文'), ] if self.user_has_groups('base.group_system'): res_filter += [('english', '英语')] elif self._is_principal: res_filter = res_filter else: res_filter = [] return res_filter
四、动态创建 selection 内容
-
创建 filter 字段
filter = fields.Selection(string=u'等级', selection='_selection_filter')
-
创建_selection_filter函数
定义一个空的res_filter列表,在ir.config_parameter(系统参数)模型下进行搜索 符合issue_filter_的键值对,并将筛选到的数据加入列表并返回@api.model def _selection_filter(self): res_filter = [] filter_list = self.env['ir.config_parameter'].search([('key', 'like', 'issue_filter_')]) for filter in filter_list: res_filter.append((filter.key[13:], _(filter.value))) return res_filter
-
打开字段对应的from视图,等级字段的selection内容还是空的。这时我们激活开发者模式,进入系统设置,创建我们需要的selection内容。具体操作如下图显示:
-
进入系统参数,创建键值对。issue_filter_ 是定义函数式进行模糊查询的字符串 one是selection内容中的键,同理可创建多个符合设定的selection内容。
-
创建好相应的键值对后,刷新界面,效果如下图:
结束,以上就是 Odoo Selection 字段的三种定义方式。