Odoo Help


How do I create a field with a running count?

on 1/6/15, 5:57 AM 2,865 views

In the product.product module, I wish to create a field (or edit an existing field) which displays an unique numeric value for each product. I want the module to automatically keep count and suggest the next number in the series. For instance, when I create a new product, I want the field to be automatically assigned the number '20000', and when I create the next product the number '20001' etc. Is there an easy way to handle this?

Thank you, Nimesh. Do you know if there is a guide to creating and assigning sequences?

on 1/6/15, 6:59 AM

Anil R. Kesariya

--Anil R. Kesariya--
| 7 6 8
Rajkot, India
--Anil R. Kesariya--

Odoo Techno Functional Expert

* Engaged with odoo since May - 2012.

* Customise existing module or develop new module base on customer requirement.

* Played key developer role in many past and running projects.

* Expert in requirement gathering of business and transforming requirement into odoo platform.

* Major expertise in core modules, database, report development and theme development.

* Integration expert.

Linkedin  : https://in.linkedin.com/in/anil-kesariya-97b20a66

Anil R. Kesariya
On 1/6/15, 7:23 AM

Hello philjun,

Here you go!

You need to follow these two steps.

Solution : 1 (In this solution your sequence field will be editable)

Step1 : Add sequence record in your module.

     <!-- Record for your seuqence type -->
        <record id="your_custom_sequence_type" model="ir.sequence.type">
            <field name="name">Label of your sequence code.</field>
            <field name="code">test.test.code</field>   <!-- Unique sequence code.-->
    <!-- Record for Sequence-->
        <record id="student_reg_sequence" model="ir.sequence">
            <field name="name">Student Unique ID</field>
            <field name="code">test.test.code</field>  <!-- Apply the same you applied above-->
            <field name="prefix">%(year)s/</field> <!-- optional-->
            <field name="suffix">%(month)s/</field> <!-- optional-->
            <field name="number_next_actual">20000</field> <!-- optional, if you not add this field by default 1 will be starting no. -->
            <field name="padding">5</field> <!-- optional-->
            <field name="implementation">no_gap</field>

Step 2: Add field in your model, in your case product is model

    _inherit = 'product.product'
    _columns = {


    _defaults = {
    'sequence':lambda self, cr, uid, context:self.pool.get('ir.sequence').get(cr, uid, 'test.test.code'),


Solution : 2 (If you want to make your sequence field readonly than you can follow this solution.)

Step 1 : This is same as Solution 1.
Step 2 : # inherit model and addd field with readonly attribute

    _inherit = 'product.product'
    _columns = {
    'sequence':fields.char("Sequence", readonly=True)


    def generate_sequence(self, cr, uid, ids, context=None):
        # This line will generate next sequence number from your seuqence.
        next_seq = self.pool.get('ir.sequence').get(cr, uid, 'test.test.code')

        self.write(cr, uid, ids, {'sequence':next_seq}, context=context)

        return True

Step 3: add button in product form

      <button name="generate_sequence" type="object" string="Generate Sequence"/>

There are many more option to create sequence these two solution will satisfied your needs.

Hope this will helps you.

Anil Kesariya

The sequence incrementing 2 ,4 ,6,etc.. .I want to generate 1 ,2 ,3 etc..Then How to change this code

on 1/12/15, 5:15 AM

Also one more problem is if I am not saving any record its incremented automatically to next id .

on 1/12/15, 5:17 AM

check you have applied : no_gap

Anil R. Kesariya
on 1/12/15, 5:20 AM

Once the sequence is generated is will always move to next sequence, so choose the second option generate it on button click and hide the button once it is generated.

Anil R. Kesariya
on 1/12/15, 5:24 AM

Here What is the excecution and what is the use of this test.test.code

on 1/12/15, 6:26 AM

Here What is the excecution flow and what is the use of test.test.code means

on 1/12/15, 6:26 AM

it is sequence code, you can give any name here, make sure that code is unique not used for any other sequence.

Anil R. Kesariya
on 1/12/15, 6:35 AM
On 1/6/15, 6:02 AM

You can create sequence starting with 20000 and assign to that field.




On 1/12/15, 4:24 AM

Thank you, Anil. I have done as specified in option 1, except that I have added the code to the existing product module rather than creating a new module. I have added the field and the default setting to the product.py file.

Regarding step 1, adding the sequence record to the module, I have created the file product_sequence.xml with the xml-code you specified, placed the file in the Product-module-folder, and added this file in the 'data' section of the __openerp__.py-file pertaining to the Product-module. Is it necessary to do something else to add the sequence record? In any case, my sequence is still not working.

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 1/6/15, 5:57 AM
Seen: 2865 times
Last updated: 3/16/15, 8:10 AM