Odoo Help

Welcome!

This community 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.

0

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

By
E.M.
on 7/8/15, 6:16 PM 2,956 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.

3

Emipro Technologies Pvt. Ltd.

--Emipro Technologies Pvt. Ltd.--
Tiny ERP Pvt Ltd
http://www.odoo.com
5975
| 6 7 8
Rajkot, India
--Emipro Technologies Pvt. Ltd.--

Being an emerging leader in IT market since 2011, Emipro Technologies Pvt. Ltd. has been providing a wide range of business solutions in Odoo & Magento. We are pleased to have a large pool of contented customers with our meticulous work in the domain of ERP & e-Commerce. A ray of relief & satisfaction to  our customers heart by our successful deployment in their organization, purely represents our success in Odoo platform. Hence, we take pride for being an Odoo partner with a vision of expanding our strategic alliances with our customers to offer them high value-added, trustworthy & cost effective solutions.

 

Since establishment, our business has span across 11 countries of the world. Our customers are companies of all sizes ranging from start-ups to large enterprises, who realize that they need a professional business solutions to generate revenue streams, establish proper communication channels or streamline business operations. Standing with strong determination of customer satisfaction, observing each minute detail of their business processes, providing proper guidance and moving forth to develop product accordingly adds our value and reliability among our customers. 

 

We are blessed with efficient, passionate & eclectic young developers who have come across almost all kinds of business profiles, working with full dedication, applying creativity & new features in existing modules and completing customer's projects successfully on predefined target. Our in-depth knowledge while giving business solutions in Odoo allows us to offer following services to our customers :

 

  • Consulting
  • Installation, Configuration & Customization
  • Training & Support
  • Maintenance
  • Upgradation
  • Documentation
  • Crafting community modules
  • Drafting videos on demand
  • Smart Apps development

We will be :) to welcome you @ www.emiprotechnologies.com to solve any kind of your business needs around Odoo. However we are just an inch away from you by email info@emiprotechnologies.com or a tweet to @EmiproTech

Emipro Technologies Pvt. Ltd.
On 7/9/15, 2:02 AM

Hi,

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
else:
    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,

E.M.
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.

E.M.
on 7/10/15, 1:06 AM
0
E.M.
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'

 

        @api.model

        def create(self, vals):

                print "DEBUG: CREATE 1"

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

                print seq_id

                seq_id._next

                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.

E.M.
on 7/9/15, 6:11 PM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 7/8/15, 6:16 PM
Seen: 2956 times
Last updated: 10/29/15, 4:45 AM