How to Auto Generate Name Column with Sequence in Odoo

In Sale Order, Sequence is a column that auto-generates numbers for new orders. This column can be used to keep track of the order in which sales were made. The sequence starts with the number 1 and increments by one for each new order.

In this blog post, you will learn how to generate a Name column with Sequence in Odoo. You can use this technique for any table where you want to automatically generate unique names for each record. This can be particularly useful when you create a custom model and needs the name to auto-increase.

We need to override the create() method and update the Name column wit

class MyModel(models.Model):
    _name = 'my.model'
    _description = 'My Model'

    name = fields.Char(string="Name", readonly=True, select=True, copy=False, default='New')

    @api.model
    def create(self, vals):
       if vals.get('name', 'New') == 'New':
           vals['name'] = self.env['ir.sequence'].next_by_code('my.model') or 'New'
       result = super(MyModel, self).create(vals)
       return result

Define a sequence to use for the my.model in an XML file:

<record id="my_model_sequence" model="ir.sequence">
    <field name="name">My Model Sequence</field>
    <field name="code">my.model</field>
    <field name="active">TRUE</field>
    <field name="prefix">TEST-</field>
    <field name="padding">4</field>
    <field name="number_next">1</field>
    <field name="number_increment">1</field>
</record>

This Sequence generates TEST-0001 as the starting point. Then it uses TEST-0002, TEST-0003, and so on.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close