-
For this we are creating a new module named
employee_custom
for better understanding. -
To inherit an existing table in the
hr.employee
model we have to add module name independs
in the manifest file. Here we are inheriting the employee module which name ishr
.'depends': ['base', 'hr']
-
Then create a python model to inherit the employee module and add some fields to it. Don't forget to import the python file in
models
>__init__.py
.class InheritCertification(models.Model): _inherit = "hr.employee" skype_name = fields.Char("Skype name") joining_date = fields.Date("Joining Date")
-
Then create an
xml
file namedinherit_certificate_views.xml
and copy this code below. Herename
will behr.view_employee_form
,model
will behr.employee
andinherit_id
>ref
will behr.view_employee_form
as we are inheriting the from view ofhr.employee
. -
Go to
Bug icon > Edit View: Form
to find the required fields.<record id="employee_inherit_id" model="ir.ui.view"> <field name="name">hr.view_employee_form</field> <field name="model">hr.employee</field> <field name="inherit_id" ref="hr.view_employee_form"/> <field name="arch" type="xml"> <xpath expr="//field[@name='work_email']" position="after"> <field name="skype_name"/> </xpath> <xpath expr="//field[@name='resource_calendar_id']" position="before"> <field name="joining_date"/> </xpath> </field> </record>
-
Add the xml file to the
__manifest__.py
. Install theemployee_custom
module and go to thehr.employee
model and you will get the added field like below.
-
Now our goal is to create a new notebook in the employee module something like this:
-
For this we have to create fields based on the requirements. As
certificate_name
,issued_by
areMany2one
field we have to create separate new classes to store those information.class EmployeesCertification(models.Model): _name = 'employee.certificate' _description = "Employee Certification" certificate_name = fields.Many2one('employee.certificate.name', string="Certificate name", required=True) issued_by = fields.Many2one('employee.certificate.issued', string="Issued by", required=True)
class CertificationName(models.Model): _name = 'employee.certificate.name' _description = "Employee Certification name" name = fields.Char(string="Certificate name") class CertificationIssued(models.Model): _name = 'employee.certificate.issued' _description = "Employee Certification Issued" name = fields.Char(string="Certificate Issued by")
-
Try to add other fields
start_date
,end_date
asDate
field,status
asSelection
field andupload_file
asBinary
field.start_date = fields.Date(string="Start date") end_date = fields.Date(string="End date") date_diff = fields.Char(string="Duration", readonly=True) status = fields.Selection([ ('complete', 'Complete'), ('progress', 'In progress'), ('certified', 'Certified') ], tracking=True, required=True) upload_file = fields.Binary(string="Upload file")
-
To add or view all those information in inherited fields, we need to add a
One2many
relationship in the inherited class with corresponding employee havingMany2one
relationship in the parent class.class InheritCertification(models.Model): _inherit = "hr.employee" certificate_ids = fields.One2many('employee.certificate', 'employees_id', string="Certificate")
class EmployeesCertification(models.Model): _name = 'employee.certificate' employee_id = fields.Many2one('hr.employee', string="Certificate")
-
Finally we have to create an
xpath
in the view file to show the fields in theemployee
module by creating a new notebook.<xpath expr="//notebook/page[1]" position="after"> <page string="Certification"> <field name="certificate_ids"/> </page> </xpath>
-
This is the final output of the inherited module after creating the new notebook.
-
If we want to replace fields or notebook we have to use
position="replace"
. We can usebefore
andafter
asposition
also. Forexpr
we can usefield
,group
,notebook
. Forfield
andgroup
we can usename
and fornotebook
we have to usepage[position]
to find the position of the field. Example for group replace in xml:<xpath expr="//group[@name='hide_basic_information']" position="replace"> </xpath>