Odoo Help


How to get the next value of a sequence in an Odoo python model

on 7/8/15, 6:16 PM 4,913 views

What should be done to get the next free value of a defined sequence inside an Odoo python model's method?

Is there any example somewhere?

I have seen next_by_id and next_by_code methods on ir_sequence.py but I am not sure on how to invoke them (not even sure that they are the right methods to invoke).

I assume that to get the right sequence I have to use: .

 seq_id = self.pool.get('ir.sequence').search(cr, uid, ['TESTSEQ'])

where TESTSEQ is the name of my defined sequence.



You can use both of the method. Please have a look on the below example.

if journal.sequence_id:
    next_number=sequence_obj.next_by_id(cr, uid, journal.sequence_id.id, context=context) # Case 1
    next_number=sequence_obj.next_by_code(cr, uid, 'account.cash.statement', context=context) #Case 2

In above Case 1 : There is sequence is stored inside journal record. So, we can use next_by_id method by providing id of any sequence.

In above Case 2 : There is "next_by_code" method is called. So, inside that method we need to pass code of sequence. In that case "account.cash.statement" is code inside one of the sequence. 

Reference : You can find out about line of code inside point_of_sale => wizard => pos_open_statement.py file.

I think now you will know how to do it.


Maybe I did not properly understand sequences. I want to create a sequence to generate consecutive numbers each time a product is generated (ultimate goal is to generate EAN13 codes for products, but I am just starting from the basics being able to print sequence numbers with PRINT). I have created a new sequence in the GUI (Configuration -> Sequences and IDs -> Sequences), -sure it has to be done via XML in the module, but just trying to understand the basics here-. Do I have to create both a sequence and a secuence code? What are they each of those ones used for? Also, as per below answer, it seems that cr and uid, do not apply in this case (it is api.model), how next_by_id and next_by_code should be used? Thanks,

on 7/9/15, 6:17 PM

I finally found that what I was looking for was _next() method. I also found that I have to create both a sequence and a sequence type and search by sequence type identifier.

on 7/10/15, 1:06 AM
On 7/9/15, 4:23 AM

This is probably a quite basic question, but while trying to get the sequence ID:

seq_id = self.pool.get('ir.sequence').search(cr, uid, ['TESTSEQ'])

I get global name 'cr' not defined.

Is the search method properly invoked?

Complete code:

# -*- coding: utf-8 -*-

from openerp import models, fields, api

class EanAuto ( models.Model ):

        _inherit = 'product.product'



        def create(self, vals):

                print "DEBUG: CREATE 1"

                seq_id = self.pool.get('ir.sequence').search(cr, uid, ['TESTSEQ'])

                print seq_id


                new_product = super(EanAuto, self).create(vals)

                print "DEBUG: CREATE 2"

                return new_product

You have used @api.model decorator. So, you do not need to pass cr, uid in search method. (Directly cr, uid is not available). You need to call search as like self.env['ir.sequence'].search([('code','=','TESTSEQ')]). I hope you can get this.

Emipro Technologies Pvt. Ltd.
on 7/9/15, 5:21 AM

That solution works, thanks.

on 7/9/15, 6:11 PM

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: 7/8/15, 6:16 PM
Seen: 4913 times
Last updated: 10/29/15, 4:45 AM