Lars Trey
Lars Trey

Reputation: 143

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?

Upvotes: 3

Views: 1066

Answers (2)

Fahim Foysal
Fahim Foysal

Reputation: 1

thanks for the answer and feedback. on the other hand(which is in my case) if someone want to remove just specific suggested fields the code can be rewritten like this:

@api.model
def fields_get(self, allfields=None, attributes=None):
    res = super().fields_get(allfields, attributes=attributes)
    fields_to_remove_from_custom_search = ['field_1','field_2']
    for field in fields_to_remove_from_custom_search:
        if res.get(field):
            res[field]['searchable'] = False
            res[field]['sortable'] = False
    return res

Upvotes: 0

Lars Trey
Lars Trey

Reputation: 143

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.

Upvotes: 5

Related Questions