콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다

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
28881
0
1월 25
1120
3
7월 23
5008
1
10월 22
3084
1
6월 22
4024