Selection字段類型一般作為下拉列表顯示
selection的選項內(nèi)容基本是固定,但是針對一些特殊要求
- 根據(jù)權(quán)限組顯示不同的selection內(nèi)容的
- 也可以通過動態(tài)創(chuàng)建進(jìn)行選項內(nèi)容的設(shè)置搂根。
1. 常規(guī)的Selection內(nèi)容(固定的)
image.png
2. 權(quán)限組顯示不同的selection內(nèi)容
selection=’_selection_filter’,表示引用_selection_filter函數(shù)進(jìn)行不同權(quán)限組之間的內(nèi)容顯示
_selection_filter函數(shù)的定義踢星。
image.png
res_filter = [
(‘none’, _(‘All products’)),
(‘category’, _(‘One product category’)),
(‘product’, _(‘One product only’)),
(‘partial’, _(‘Select products manually’))]
為selection內(nèi)容初始化的列表
后面的判斷語句隙咸,則是根據(jù)當(dāng)前用戶所在的權(quán)限組進(jìn)行selection內(nèi)容的添加
例如:
if self.user_has_groups('stock.group_tracking_owner'),用戶存在stock模塊下定義的group_tracking_owner組中藏否,則在原有的列表中加入’owner’,'product_owner’這兩個鍵值對內(nèi)容副签,
res_filter += [('owner', _('One owner only')), ('product_owner', _('One product for a specific owner'))]
將用戶放入不同的權(quán)限組則可以看到不同的selection下拉內(nèi)容
3.動態(tài)創(chuàng)建selection內(nèi)容
創(chuàng)建 group 字段
group = fields.Selection(string='物料分組', selection='_selection_group')
創(chuàng)建_selection_group函數(shù)
定義一個空的sort_group列表基矮,在smelting.ingredients(自己的模型)模型下進(jìn)行搜索 ,并將篩選到的數(shù)據(jù)加入列表并返回
此_selection_group是針對模型的操作家浇,@api.model,不能對記錄集進(jìn)行操作
image.png
效果如下:
制作不易点额,點贊鼓勵哈