Search code examples
pythonxmlinheritanceodooodoo-10

Add fields to hr.employees


Trying to add fields to the model hr.employees (cmp after address_home_id)

This is my code.

__manifest__.py

'name': 'Add Fields to HR module',
'description': 'Add Fields to HR Module',
'depends': ['base','hr'],
'data':[
  'views/res_hr_view.xml',  
],

res_hr.py

 from odoo import models, fields, api 
 class ResHR(models.Model):
    _inherit = 'hr'
    cmp = fields.Char()

res_hr_view.xml

 <?xml version="1.0"?>
  <odoo>
    <record id="res_hr_form_inherited"   
     model="ir.ui.view"> 
     <field name="name">Add Fields HR Employees</field> 
     <field name="model">hr.employee</field> 
     <field name="inherit_id" ref="hr.view_employee_form"/> 
     <field name="arch" type="xml"> 
     <field name="address_home_id" position="after"> 
       <field name="cmp" string="C.M.P"/>
       </field>
    </field>
   </record>

but show me te next error when I try to install it.

   Field 'cmp' does not exist error on inherited view.
   > /home/odoo/odoo-dev/odoo/odoo/models.py(1083)_validate_fields()
   -> raise ValidationError("%s\n\n%s" % (_("Error while validating 
   constraint"), tools.ustr(e)))

what am I doing wrong ?


Solution

  • Make sure your res_hr.py is imported. Normally a module/app looks like

    models/
    |-- __init__.py
    |-- model1.py
    |-- model2.py
    views/
    |-- model1_views.xml
    |-- model2_views.xml
    __init__.py
    __manifest__.py
    

    The __init__.py should import models and the models/__init__.py should import model1 and model2. Restart the server when ready for module installation/update or use the dev-mode to let Odoo handle it itself.