odoo selection字段类型的动态添加

23 篇文章 0 订阅
15 篇文章 0 订阅

Selection字段类型一般作为下拉列表显示
selection的选项内容基本是固定,但

  • 根据权限组显示不同的selection内容的
  • 也可以通过动态创建进行选项内容的设置
1. 常规的Selection内容(固定的)

在这里插入图片描述

2. 权限组显示不同的selection内容

selection=’_selection_filter’,表示引用_selection_filter函数进行不同权限组之间的内容显示

_selection_filter函数的定义。
image.png
为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]

image.png

效果如下:

制作不易,点赞鼓励哈

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
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

隔壁小红馆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值