
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.
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.
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.
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:
First, you need to define the sequence in an XML file. Here's an example:
<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:
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:
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:
next_by_code()
function, passing the sequence code (custom.order) defined in XML.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:
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.
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:
@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.
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.
Your email address will not be published. Required fields are marked *