Selection字段类型一般作为下拉列表显示
selection的选项内容基本是固定,但
- 根据权限组显示不同的selection内容的
- 也可以通过动态创建进行选项内容的设置
1. 常规的Selection内容(固定的)
2. 权限组显示不同的selection内容
selection=’_selection_filter’,表示引用_selection_filter函数进行不同权限组之间的内容显示
_selection_filter函数的定义。
为selection内容初始化的列表
后面的判断语句,则是根据当前用户所在的权限组进行selection内容的添加
例如:
if self.user_has_groups(‘stock.group_tracking_owner’),用户存在stock模块下定义的group_tracking_owner组中,则在原有的列表中加入’owner’,'product_owner’这两个键值对内容,
res_filter += [('owner', _('One owner only')), ('product_owner', _('One product for a specific owner'))]
将用户放入不同的权限组则可以看到不同的selection下拉内容
3.动态创建selection内容
创建 group 字段
group = fields.Selection(string=‘物料分组’, selection=’_selection_group’)
创建_selection_group函数
定义一个空的sort_group列表,在smelting.ingredients(自己的模型)模型下进行搜索 ,并将筛选到的数据加入列表并返回
此_selection_group是针对模型的操作,@api.model,不能对记录集进行操作
@api.model
def _selection_group(self):
sort_group =[]
for smelting in self.env['smelting.ingredients'].search([]):
for table in smelting.ingredients_table:
sort_group.append(table.groups)
sort_group.sort()
sort_group.append('all')
return [(rec, rec) for rec in sort_group]
效果如下:
制作不易,点赞鼓励哈
4.动态创建selection加计算compute
view_search = fields.Selection(string='类别分组', selection='_selection_group', store=True)
@api.model
def _selection_group(self):
sort_group = []
for rec in self.env['production.titanium.blank'].search([('titanium_blank', '=', 'blank')]):
if rec.categ_id and rec.categ_id not in sort_group:
sort_group.append(rec.categ_id)
return [(rec.display_name, rec.display_name) for rec in sort_group]
@api.depends('categ_id')
def _compute_categ_id(self):
for rec in self:
rec.view_search = rec.categ_id.name