Search code examples
pythonodooodoo-13

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


I want to remove a lot of the standard filter properties which appear in "Add custom filter..." and "Add custom group..." on a Tree-View (here: hr.employee.tree).

The filter properties which appear for selection, are obviously all fields in the associated model of the Tree-View, but I don't need all of them.

I figured out a very promising way, which actually works in regard of removing these properties from the Filter/Grouping, but raises exceptions when saving a create/edit on the Form-View of the same model:

## 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

Exception when saving on Form-View for basically every field I touched in the loop above:

[...]
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'

It seems like the exception occurs for tracked-fields (mail_thread.py).

Any ideas?


Solution

  • I figured it out already. This is the solution:

    @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
    

    Maybe this is a help for other people, too.