Search code examples
pythonxmlinheritanceodooodoo-12

Odoo. Why my inheritance of 'hr.employee' model is working bad?


I need to create a new model, something like "hr.employee.lines" in order to assign 1 or more employees to an specific employee. Like a hierarchical structure.

So, on my hr_employee.py file I have this:

from odoo import fields, models, api, _

class Employees(models.Model):
    _inherit = 'hr.employee'

    approvers = fields.One2many('hr.employee.approvers', 'employee_id')

class EmployeeApprovers(models.Model):
    _name = 'hr.employee.approvers'

    employee_id = fields.Many2one('hr.employee')
    lower_limit = fields.Integer('Lower limit')
    upper_limit = fields.Integer('Upper limit')

On the DB everythin looks fine. Problem is when I try to add a 'hr.employee.approvers' in the 'hr.employee' form.

In this picture you can see what I want to add, before I save it. In this picture you can see what I want to add, before I save it.

And, when I save it, the employee change to the name of the actual employee.

When I save it, the employee change to the name of the actual employee.

I think there is no problem is XML, but I could be wrong so I'll put it tho.

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>
        <record id="view_hr_employee_form_inherit" model="ir.ui.view">
            <field name="name">hr.employee.form.inherit</field>
            <field name="model">hr.employee</field>
            <field name="inherit_id" ref="hr.view_employee_form"/>
            <field name="arch" type="xml">
                <xpath expr="//page[@name='public']" position="inside">
                    <group string="Aprobadores">
                        <field name="approvers">
                            <tree string="Aprobadores" editable="bottom">
                                <field name="employee_id"/>
                                <field name="lower_limit"/>
                                <field name="upper_limit"/>
                            </tree>
                        </field>
                    </group>
                </xpath>
            </field>
        </record>
    </data>
</odoo>

I will appreciate all the help.


Solution

  • One2many field:
    The value of such a field is the recordset of all the records in comodel_name such that the field inverse_name is equal to the current record.

    You are editing the value of the inverse_name field, the value of employee_id in hr.employee.approvers model record should be set to Eli Lambert record id.

    You will need to add another field approver_id to reference hr.employee and show it instead of employee_id (make employee_id field invisible in the form view).

    If the approver is an employee with lower and upper limits, add the two limit fields to the employee model, and use a Many2many field instead.