How to Implement Custom Sequence Generation with ir.sequence  in Odoo?
✅ What is ir.sequence

In Odoo, ir.sequence is a model used to generate unique and structured numbers, commonly for documents such as invoices, orders, or custom records. These sequences help maintain readable, consistent, and traceable references.

📌 When to Use Custom Sequences?

  • Generating unique numbers for custom models (e.g., customer.ticket, asset.tag)
  • Adding prefixes or date-based formatting to identifiers
  • Ensuring sequence continuity across companies or configurations

🛠 Step-by-Step: Define a Custom Sequence for a Model

1. Define a Sequence Record in XML

<record id=”seq_customer_ticket” model=”ir.sequence”>

    <field name=”name”>Customer Ticket Sequence</field>

    <field name=”code”>customer.ticket</field>

    <field name=”prefix”>TIC/%(year)s/</field>

    <field name=”padding”>5</field>

    <field name=”number_next”>1</field>

    <field name=”number_increment”>1</field>

    <field name=”use_date_range”>False</field>

</record>

  • code: A unique identifier used to call the sequence
  • prefix: Can include date variables like %(year)s, %(month)s
  • padding: Number of digits (e.g., 00001)
  • use_date_range: If set to True, the sequence restarts monthly/yearly

2. Use It in Your Model’s create() Method

from odoo import models, fields, api


class CustomerTicket(models.Model):

    _name = ‘customer.ticket’

    _description = ‘Customer Ticket’


    name = fields.Char(string=‘Ticket Number’, required=True, copy=False, readonly=True, default=’New‘)

    subject = fields.Char(string=’Subject‘)


    @api.model

    def create(self, vals):

        if vals.get(‘name’, ‘New’) == ‘New’:

            vals[‘name’] = self.env[‘ir.sequence’].next_by_code(‘customer.ticket’) or ‘/’

        return super(CustomerTicket, self).create(vals)

  • If no custom name is provided, it auto-generates one using the defined sequence.
  • next_by_code(‘customer.ticket’) fetches the next number in the sequence.

🔄 Example Output

If your prefix is TIC/%(year)s/ and padding is 5, the generated name would look like:
TIC/2025/00001
TIC/2025/00002

🔐 Multi-Company / Date Range Sequences

  • Sequences can be company-specific by setting the company_id.
  • Enable date ranges to reset yearly (e.g., invoice sequences).

✅ Summary Table

Feature
ir.sequence Capability
Custom prefix ✅ Yes  (%(year)s, %(month)s)
Padding (e.g., 00001) ✅ Yes
Per-company support ✅ Yes
Date-based reset ✅ Yes (use_date_range=True)
API Usage next_by_code(code)