How to Create Sequence & Generate Values in Odoo

Abid Patel
10-Oct-2024 Updated : 10-Oct-2024

Learn how to create sequences and generate sequential values for records in Odoo using the ir.sequence model. Step-by-step guide with examples included.

How to Create Sequence & Generate Values in Odoo

How to Create Sequence and Generate Sequential Value for Records in Odoo

In Odoo, sequences are widely used to automatically generate unique and sequential values for records, such as invoice numbers, purchase order references, and more. Using Odoo’s ir.sequence model, developers can create custom sequences to ensure that records are numbered correctly and consistently. In this blog post, we will guide you through the steps to create a sequence in Odoo and generate sequential values for records.

What is a Sequence in Odoo?

A sequence in Odoo is a system that generates unique, sequential values (such as numbers or alphanumeric strings) that can be assigned to records. The sequence is defined using the ir.sequence model, and Odoo automatically increments the value each time a new record is created. Sequences are essential for keeping track of important documents, ensuring unique identifiers, and maintaining a consistent numbering scheme across records.

How to Create a Sequence in Odoo

To create a sequence in Odoo, we need to define a record in the ir.sequence model. This sequence can then be used to assign a sequential value to a specific field in a model. Let’s look at the steps to create a sequence:

Step 1: Define a Sequence in XML

First, you need to define the sequence in an XML file. Here's an example:

xml

<record id="custom_order_sequence" model="ir.sequence">
    <field name="name">Custom Order Sequence</field>
    <field name="code">custom.order</field>
    <field name="prefix">ORD/</field>
    <field name="padding">5</field>
    <field name="number_next">1</field>
    <field name="number_increment">1</field>
</record>

In this example:

  • ▹ name: The name of the sequence (for identification).
  • ▹ code: A unique code to identify where the sequence will be used.
  • ▹ prefix: A prefix to be added before the sequential number (e.g., "ORD/").
  • ▹ padding: The number of digits in the sequential value (e.g., 00001, 00002).
  • ▹ number_next: The next number that will be assigned.
  • ▹ number_increment: The increment between each number (typically 1).

Step 2: Add the Sequence in the Python Model

Once the sequence is defined in XML, we need to modify the Python model to use this sequence when creating new records. Here’s how you can achieve this:

Python

from odoo import models, fields, api

class CustomOrder(models.Model):
    _name = 'custom.order'
    _description = 'Custom Order'

    name = fields.Char(string='Order Reference', required=True, copy=False, readonly=True, index=True, default=lambda self: _('New'))

    @api.model
    def create(self, vals):
        if vals.get('name', _('New')) == _('New'):
            vals['name'] = self.env['ir.sequence'].next_by_code('custom.order') or _('New')
        return super(CustomOrder, self).create(vals)

In this example:

  • ▹ We define a model called custom.order with a field name to store the order reference.
  • ▹ In the create method, we check if the name field has the default value "New". If it does, we generate a new value using the ir.sequence model's next_by_code() function, passing the sequence code (custom.order) defined in XML.

Generating Sequential Values for Records

Once the sequence is defined and linked to the model, every time a new record is created, Odoo will automatically generate a sequential value for the name field based on the sequence definition. For example:

  • ▹ The first record will have the reference ORD/00001
  • ▹ The second record will have the reference ORD/00002
  • ▹ And so on...

Configuring Sequences for Different Models

Sequences in Odoo can be configured for any model. Whether you’re generating unique invoice numbers, purchase order references, or customer codes, the process is the same. Simply define the sequence in XML, and override the create method in the corresponding model to apply the sequence to the desired field.

Using Multiple Sequences for Different Conditions

In some cases, you may want to use different sequences based on specific conditions. For instance, you might want different prefixes for local and international orders. Here’s an example of how to handle that:

python

@api.model
def create(self, vals):
    if vals.get('order_type') == 'international':
        vals['name'] = self.env['ir.sequence'].next_by_code('custom.order.international') or _('New')
    else:
        vals['name'] = self.env['ir.sequence'].next_by_code('custom.order.local') or _('New')
    return super(CustomOrder, self).create(vals)

In this case, we use different sequences depending on the value of the order_type field, generating distinct references for local and international orders.

Conclusion

Creating a sequence and generating sequential values for records in Odoo is an essential feature for ensuring consistency and uniqueness across your business processes. Whether you’re generating invoice numbers, order references, or any other unique identifier, Odoo’s ir.sequence model provides a powerful and flexible way to manage sequential numbering. By following the steps outlined in this blog, you can easily define and implement sequences in your own custom models.

For more Odoo development tips and tricks, be sure to check our website Free web snippets.

Make a Comment

Your email address will not be published. Required fields are marked *