DEV Community

Cover image for Understanding Sequence Creation in Odoo
Jeevachaithanyan Sivanandan
Jeevachaithanyan Sivanandan

Posted on • Updated on

Understanding Sequence Creation in Odoo

In the realm of Odoo, a powerful open-source business management software, the creation and management of sequences play a crucial role in maintaining structured and organized data. Sequences are commonly used for generating unique identifiers for records, such as invoice numbers, customer IDs, or any other incremental values. This article delves into the technical aspects of sequence creation in Odoo, focusing on how to define, configure, and utilize sequences effectively.

Defining Sequences in XML

The first step in creating a sequence in Odoo is defining it in XML. Sequences are typically defined in the module's data files, usually within a specific XML file. The most common attributes for sequences in Odoo are as follows:

name: Specifies the display name of the sequence.

<field name="name">My Sequence</field>
Enter fullscreen mode Exit fullscreen mode

code: Sets a unique code for the sequence. This code is used to reference the sequence in the Python code.

<field name="code">my_sequence_code</field>
Enter fullscreen mode Exit fullscreen mode

implementation: Defines the type of sequence. The possible values are 'standard', 'no_gap', 'easy', and 'prefix'.

<field name="implementation">standard</field>
Enter fullscreen mode Exit fullscreen mode

padding: Specifies the number of digits the sequence should have. The sequence values are padded with zeros to match this length.

<field name="padding">5</field>
Enter fullscreen mode Exit fullscreen mode

prefix: Sets a prefix to be added to each generated sequence value.

<field name="prefix">ABC-</field>
Enter fullscreen mode Exit fullscreen mode

suffix: Adds a suffix to each generated sequence value.

<field name="suffix">-XYZ</field>
Enter fullscreen mode Exit fullscreen mode

start: Defines the initial value of the sequence.

<field name="start">1000</field>
Enter fullscreen mode Exit fullscreen mode

increment: Sets the amount by which the sequence value is incremented each time.

<field name="increment">1</field>
Enter fullscreen mode Exit fullscreen mode

use_date_range: If set to True, the sequence will reset to its initial value when the date changes.

 <field name="use_date_range" eval="True"/>
Enter fullscreen mode Exit fullscreen mode

number_next: Specifies the next value that the sequence will generate. This can be useful when migrating data.

<field name="number_next">500</field>
Enter fullscreen mode Exit fullscreen mode

number_increment: Sets the increment value for the number_next field.

<field name="number_increment">5</field>
Enter fullscreen mode Exit fullscreen mode

Here's an example of an XML code defining a sequence for patient IDs in a hospital with various attributes: Usually this will be saved as data.xml in the module.

<?xml version='1.0' encoding='utf-8'?>
<odoo>
    <data>
        <record id="sequence_patient" model="ir.sequence">
            <field name="name">Patient ID Sequence</field>
            <field name="code">patient_id_sequence</field>
            <field name="implementation">standard</field>
            <field name="padding">4</field>
            <field name="prefix">PID-</field>
            <field name="suffix">-HOSP</field>
            <field name="start">1000</field>
            <field name="increment">1</field>
            <field name="use_date_range" eval="True"/>
            <field name="number_next">2000</field>
            <field name="number_increment">2</field>
        </record>
    </data>
</odoo>

Enter fullscreen mode Exit fullscreen mode

Explanation of the attributes used in this example:

  • name: "Patient ID Sequence" - Display name for the sequence.
  • code: "patient_id_sequence" - Unique code to reference the sequence.
  • implementation: "standard" - Standard sequence implementation.
  • padding: 4 - Sequence values will be padded with zeros to have a length of 4 characters.
  • prefix: "PID-" - Prefix added to each generated sequence value.
  • suffix: "-HOSP" - Suffix added to each generated sequence value.
  • start: 1000 - Initial value of the sequence.
  • increment: 1 - The sequence value is incremented by 1 for each new record.
  • use_date_range: True - The sequence will reset to its initial value when the date changes.
  • number_next: 2000 - Specifies the next value that the sequence will generate.
  • number_increment: 2 - Increment value for the number_next field.

Now we can write the model code to create this field as below

from odoo import models, fields, api

class HospitalPatient(models.Model):
    _name = 'hospital.patient'
    _description = 'Hospital Patient'

    name = fields.Char(string='Patient Name', required=True)
    patient_id = fields.Char(string='Patient ID', readonly=True, copy=False)

    @api.model
    def create(self, values):
        if values.get('patient_id', '/') == '/':
            values['patient_id'] = self.env['ir.sequence'].next_by_code('patient_id_sequence') or '/'
        return super(HospitalPatient, self).create(values)

Enter fullscreen mode Exit fullscreen mode

notice that the argument ' patient_id_sequence ' is the code of the sequence that we created in the data.xml file.

The .next_by_code method is a method provided by the ir.sequence model in Odoo. This method is used to retrieve the next value in a sequence identified by a specific code.

self.env['ir.sequence'].next_by_code('patient_id_sequence')
Enter fullscreen mode Exit fullscreen mode
self.env['ir.sequence']: This part fetches the ir.sequence model through the Odoo environment (env). It provides access to the functionality of the ir.sequence model.

.next_by_code('patient_id_sequence'): This part calls the next_by_code method of the ir.sequence model, passing the code 'patient_id_sequence' as an argument. The code is used to identify which sequence you want to get the next value from.
Enter fullscreen mode Exit fullscreen mode

So, in summary, .next_by_code('patient_id_sequence') retrieves the next value in the sequence associated with the code 'patient_id_sequence'. The code is a way of uniquely identifying a particular sequence within the system. Each sequence created in Odoo has a unique code, and you use this code to fetch the next value from that specific sequence.

Once we have the model and data files ready, we can simply use this field in the view, and it will display the sequence as required. Sequences in Odoo provide an elegant solution for generating unique identifiers and maintaining data integrity. By defining sequences in XML and integrating them into Python models, developers can ensure the systematic generation of incremental values. Understanding the nuances of sequence creation is crucial for building robust Odoo modules that adhere to business logic and requirements.

Top comments (0)