Saturday, 4 October 2014

Odoo/OpenERP: Generate Employee Code

Employee code can be generated in following three simple steps:

1. Create a sequence file


<?xml version="1.0" encoding="utf-8"?>
        <!-- Sequences for employee code -->
        <record id="emp_code_sequence" model="ir.sequence.type">
            <field name="name">Employee Code</field>
            <field name="code">hr.employee</field>

        <record id="seq_hr_employee" model="ir.sequence">
            <field name="name">Employee Code</field>
            <field name="code">hr.employee</field>
           <field name="prefix">EMP</field>
            <field name="padding">5</field>
            <field name="company_id" eval="False"/>
        <!-- End of Sequence code for employee code -->


2. Add new field to class

class hr_employee(osv.osv):
    _name = 'hr.employee'
    _inherit = 'hr.employee'    
    _columns = {
                'emp_code':fields.char('Employee Code', size=8, readonly=True)

3. Update database

def create(self, cr, uid, vals, context=None):
        vals['emp_code']=self.pool.get('ir.sequence').get(cr, uid,'hr.employee')
        res=super(hr_employee, self).create(cr, uid, vals)
        return res

create function will be called only when new employee is added.

and you are done.

Employee code will start from EMP00001

You can generate emp_code without sequence file as well and write code directly in create function as well. But standard way is using sequence file only.

Note: Don't forget to include 'emp_code_sequence_view.xml' file in data section of '' file.

Thanks!!!!!!!!! Enjoy Programming :)


  1. Nice Article...really helped me get started with sequences

  2. i wanna work in interface how can i call a sequence in new field

  3. I want to generate the same but as a product_id in the product.template model. How can I achieve that?


Thanks for your valuable comments.