How to Create Sequence Code in Odoo

Sequence is a mechanism used to generate unique identifiers for records in a structured and automated manner in Odoo. Sequences ensure that records are systematically numbered, which used for tracking and organizing data within the system. The sequences can be customized with prefixes, suffixes, and padding to meet specific business requirements. Today, we will discuss how to create sequence in Odoo 16.
How to Create Sequence Code in Odoo

How to Create Sequence Code in Odoo 16

In Python:

First of all, you need a python class and field as below, where you have to create sequence in odoo field.

# -*- coding: utf-8 -*-
from openerp import models, fields, api

class MyPythonClass(models.Model):
_name = "python.class"
_description = "My Python Class"

sr_no = fields.Char(string="Sr No.")

After that, you have to override create function and add sequence code to create sequence in respective field as below,

@api.model
def create(self, vals):
vals['sr_no'] = self.env['ir.sequence'].next_by_code('sequence_code')
record = super(MyPythonClass, self).create(vals)
return record

In XML:

The sequence code calls from python file and runs from XML. The prefix is used to show data from sequence while we configure the sequence length by padding. In below example, the sequence will be start from IS-00001 to IS-99999. Because we add IS- prefix and 5 padding. We can add month, year, day, and time as suffix.    
<odoo>
<data>
<record model="ir.sequence" id="my_python_code_sequence">
            <field name="name">py_python_code_sequence</field>
            <field name="code">sequence_code</field>
            <field name="prefix">IS-</field>
            <field name="padding">5</field>
        </record>
        </data>
        </odoo>
        
This is a simple method to create sequence in Odoo 16. User can create sequences manually from Odoo technical setting. we hope, this article will helpful for you.
      
Previous Post Next Post