从 ODOO 树视图上的 "Add Custom Filter"/"Add Custom Group" 中删除可选择的属性

Remove selectable properties from "Add Custom Filter"/"Add Custom Group" on ODOO Tree-View

我想删除许多出现在树视图上“添加自定义过滤器...”和“添加自定义组...”中的标准过滤器属性(此处:hr.employee.tree) .

显示供选择的过滤器属性,显然是树视图关联模型中的所有字段,但我不需要所有这些。

我想出了一个非常有前途的方法,它实际上适用于从 Filter/Grouping 中删除这些属性,但在保存 create/edit 时引发异常] 在同模型的Form-View上:

## These are the fields I want to keep in "Filter by"/"Group by"
filerable_groupable_fields = ['name','phone','private_email','gender','department_id','work_email','work_phone','birthday']

@api.model
def fields_get(self, allfields=None, attributes=None):
    res = super(HrEmployee, self).fields_get(allfields, attributes=attributes) 
    not_filerable_groupable_fields = set(self._fields.keys()) - set(self.filerable_groupable_fields)
    for field in not_filerable_groupable_fields:
        res[field]['selectable'] = False ## Remove from FilterBy
        res[field]['sortable'] = False ## Remove from GroupBy
    return res

在 Form-View 上保存我在上面循环中触及的基本上每个字段时出现异常:

[...]
File "/usr/lib/python3/dist-packages/odoo/addons/hr/models/hr_employee.py", line 244, in create
    employee = super(HrEmployeePrivate, self).create(vals)
  File "<decorator-gen-105>", line 2, in create
  File "/usr/lib/python3/dist-packages/odoo/api.py", line 343, in _model_create_multi
    return create(self, [arg])
  File "/usr/lib/python3/dist-packages/odoo/addons/mail/models/mail_thread.py", line 297, in create
    tracked_fields = self._get_tracked_fields()
  File "/usr/lib/python3/dist-packages/odoo/addons/mail/models/mail_thread.py", line 554, in _get_tracked_fields
    return self.fields_get(tracked_fields)
  File "/mnt/extra-addons/custom_swaf_hr/models/hr_employee.py", line 165, in fields_get
    res[field]['selectable'] = False ## Remove FilterBy
KeyError: 'mobile_phone'

跟踪字段似乎出现异常 (mail_thread.py)。

有什么想法吗?

我已经想通了。这是解决方案:

@api.model
def fields_get(self, allfields=None, attributes=None):
    res = super(HrEmployee, self).fields_get(allfields, attributes=attributes) 
    not_filerable_groupable_fields = set(self._fields.keys()) - set(self.filerable_groupable_fields)
    for field in not_filerable_groupable_fields:
        if field in res:
            res[field]['selectable'] = False ## Remove FilterBy
            res[field]['sortable'] = False ## Remove GroupBy            
    return res

也许这对其他人也有帮助。