Odoo sequence ussually needed for automatically generated and suppose to be unique value to identified certain object or in this context is document. For example company need unique document number for every Sales document they have.
When we need to create NEW sequence in odoo ?
When we have new object and this object suppose to have unique and auto generated number. in this example we have new object called 'sale.doc' will be used to store any document related to SO such as contract, delivery doc etc.
1. create xml code to declare the sequence
2. call the sequence and set it on certain char field ('name) that belongs to the same object
When we need to create NEW sequence in odoo ?
When we have new object and this object suppose to have unique and auto generated number. in this example we have new object called 'sale.doc' will be used to store any document related to SO such as contract, delivery doc etc.
1. create xml code to declare the sequence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Here i create sequence for model 'sale.doc' with prefix 'SK'. Padding is the length of increment number. padding 5 = 00001 --> | |
<!-- Later to be used in python code for running number of 'sale.doc' --> | |
<odoo> | |
<data noupdate="1"> | |
<record id="seq_sale_doc" model="ir.sequence"> | |
<field name="name">doc sequence</field> | |
<field name="code">sale.doc</field> | |
<field name="prefix">SK</field> | |
<field name="padding">5</field> | |
</record> | |
</data> | |
</odoo> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SaleDoc(models.Model): | |
_name = 'sale.doc' | |
name = fields.Char('No Dokumen') | |
tanggal = fields.Date('Tanggal') | |
sale_id = fields.Many2one('sale.order', string="Order") | |
keterangan = fields.Text('Keterangan') | |
file = fields.Binary('File Dokumen') | |
# Magic suppose to be happen in here. NOT BUGS | |
@api.model | |
def create(self, vals): | |
# 1 GET YOUR SEQUENCE WITH LATEST INCREMENT RUNNING NUMBER | |
seq = self.env['ir.sequence'].next_by_code('sale.doc') or '/' | |
# 2 SET THE SEQUENCE ON 'NAME' FIELD | |
vals['name'] = seq | |
# 3 RETURN SUPER TO EXTEND THE CREATE METHOD | |
return super(SaleDoc, self).create(vals) |
thanks man
ReplyDeleteyour welcome
Deletehope this will helps: https://learnopenerp.blogspot.com/2020/08/generate-create-sequence-number-odoo.html
ReplyDelete