跳至内容
菜单
此问题已终结

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.

形象
丢弃
最佳答案

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,

编写者

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.

编写者 最佳答案

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.

编写者

That solution works, thanks.

最佳答案

the latest sequence value is returned by using this:

last_seq = env['your_model'].search([], order='sequence ASC')[-1].sequence
形象
丢弃
相关帖文 回复 查看 活动
2
12月 23
28080
0
1月 25
517
3
7月 23
4166
1
10月 22
2472
1
6月 22
3299