This question has been flagged

I have two selectbox field in the form employee and department. I want to pick department automatically when I pick employee according to employee's department_id.

Here is my .py code (Odoo 11)

class HrExpenseExpense(models.Model):
_inherit = "hr.expense"

department_expense_group = fields.Selection(
    string='Department Expense Group',
    related='department_id.expense_group',
    readonly=True,
)
department_id = fields.Many2one('hr.department', string='Department',
                                states={'post': [('readonly', True)], 'done': [('readonly', True)]})

@api.onchange('employee_id')
def onchange_employee_id(self):
    department_id = [x.id for x in self.employee_id.department_id]
    return {'domain': {'department_id': [('id', 'in', department_id)]}}

And xml view file:

 <xpath expr="//field[@name='employee_id']" position="after">
   <field name="department_expense_group" invisible="1" />
   <field name="department_id" invisible="0"/>
 </xpath>

It's working but problem is this; when I pick an employee, department selectbox updating automatically, it's triggering but not selecting automatically. Selectbox's list filling after I click to it. It must be selected automatically according to employee choice.

Thank you for help!

Avatar
Discard

Hello,

Yes that is because you are not updating directly the value, you are actually updating DOMAIN of field in your onchange_employee_id.

You should add line like below

if self.employee_id:

self.department_id = self.employee_id.department_id and self.employee_id.department_id.id or False

Thanks!

You can find odoo customization tips from here: https://plus.google.com/collection/4UX8UE

Author

thank you so much, that solved the problem Dipak. Thanks for the link Sehrish.