0
votes

In my module I have to create a view to set goals for departments. In create form view there is a dropdown (many2one field related to hr.department) with selection widget to select the department. I have 2 requirements:

  1. Departments dropdown should be filtered.
  2. Filtration should only occur in create view.

I tried with fields_view_get and its working fine for the create view. But the problem is filtration occurs in both form view and form update :(

This is my code used in fields_view_get:

def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
    result = super(hr_appraisal_goal_department, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu)

    if view_type=='form':
        current_user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
        dept_ids = []
        for groups in current_user.groups_id:
            if groups.name == "Define All Departmental Goals":
                dept_ids= self.pool.get('res.users').search(cr, uid, [])
            elif groups.name == "Define Departmental Goals Only to My Own Department":
                dept_ids.append(current_user.employee_ids.department_id.id)

            #there may be additional conditions in future 

        doc = etree.XML(result['arch'])
        nodes = doc.xpath("//field[@name='department_id']")
        for node in nodes:
            node.set('domain', "[('id', 'in',["+','.join(map(str, dept_ids)) +"])]")
            result['arch'] = etree.tostring(doc)

    return result

Thanks for a kind help.

1

1 Answers

0
votes

What you can take into account is that when you are in "create view", the current record (in this case a hr_appraisal_goal_department record) is going to have no ID, however, if you are editing it in an "edit view", the record has already an ID.

So you can play with this condition when setting the domain.