Enable Dark Mode!
By: Meera K

How to Add Sequence in Odoo 12

Technical Odoo 12

It's quite a simple procedure to add sequences for records in Odoo. If an individual needs to add a sequence number for every record of their model, they first have to add a field in their model and need to create a record in ir.sequence in the XML file.

For example:

In the python file add a field in your model like the code below.

name = fields.Char(string="Service Number", readonly=True, required=True, copy=False, default='New')

Then add a function like in the following code.

def create(self, vals):
   if vals.get('name', 'New') == 'New':
       vals['name'] = self.env['ir.sequence'].next_by_code(
           'self.service') or 'New'
   result = super(SelfService, self).create(vals)
   return result

After defining the function, you need to create a record for this. So create a record in the XML file .

<record id="sequence_self_service" model="ir.sequence">
   <field name="name">Self Service</field>
   <field name="code">self.service</field>
   <field name="active">TRUE</field>
   <field name="prefix">SS</field>
   <field name="padding">6</field>
   <field name="number_next">1</field>
   <field name="number_increment">1</field>

By doing this, a sequence number gets automatically generated, on creating a record in this model. 

> name- Name of the record created in the “ir.sequence” model

> code- Sequence code 

> active- A boolean field to indicate whether it is active or not

> prefix- A character field, where we can give the prefix of the sequence

> padding- sequence size

> number_next- next number that will be used

> number_increment- the next number of the sequence will be incremented by this.


In the below image -we can see a sequence number generated for the record with prefix SS and number  000001(odoo will automatically add ‘0’s to the left side of the next number to get the required padding size-here we given padding size =6, so the first sequence number becomes 000001).


We can configure the sequence number from the settings of odoo.

Go to Settings -> Technical -> Sequences & Identifiers -> Sequences

The below image shows the corresponding record of the sequence number created using the above code.


There are two types of the implementation method 

 1. No gap

 2. standard

No gap defines that there will be no gap in the numbering of records.

For example, if a self-service form is created, it will be numbered as SS000001. Then the second will be SS000002. If I deleted the second form and create a third one, then in the case of 

> No gap- sequence  will be SS000002

> Standard- sequence will be SS000003

In the prefix and suffix field, you can add a prefix and suffix of the sequence. You can define the date range for the sequence. For this, you have to check the “Use subsequences per date_range” field. 

For example, if you want to start a sequence number from 1 at the start of every month, then define the date range in the form and to the field and the next number as 1.


Odoo Blogs Odoo Development Tutorials Odoo 13 Book

If you need any assistance in odoo, we are online, please chat with us.


Akhil Kannan

name = fields.Char(string='Id-Card Number', readonly=True, required=True, copy=False, default=lambda self:self.env['ir.sequence'].next_by_code('self.service'))




Akhil Kannan

hellow sir, @api.model def create(self, vals): if vals.get('name', 'New') == 'New': vals['name'] = self.env['ir.sequence'].next_by_code( 'self.service') or 'New' result = super(SelfService, self).create(vals) return result this is working code but any idea for sequence value without create function(I mean define another function)





Is it possible to add such sequence : J/21-22(previous year & current year)/sequence





result = super(SelfService, self).create(vals) from the code below,where does SelfService come from?




Carlos Cañete

At the beginning of each day, I reset the sequence number at 1, that is because, I need you to not reset the counter!





No gap function is not working. Please add a tutorial if possible.




Leave a comment




Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635



Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.



Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message