Reputation: 267
I have a form view for sale.order.line
model and have created a field (sequence field) called ' niu '
which increases its value automatically.
For each product type stockable you add to the form which is working fine But I have a problem because when you add a product stackable the value increases, but when I press the 'Save ' button automatically changes the sequence.
See below attached Images :
This is the ' niu ' field and sale.order.line method within the model:
niu = fields.Char(string="NIU", compute="_niu_validation", readonly=True, store=True)
@api.depends('product_id.product_tmpl_id.type')
def _niu_validation(self):
for rec in self:
if rec.product_id.product_tmpl_id.type == 'product' and not rec.niu:
rec.niu = self.env['ir.sequence'].next_by_code('sale.order.line')
What can I do to make the sequence is not changed and the initial number is established ?
Upvotes: 1
Views: 2685
Reputation: 11257
It is because next_by_code()
method generate value based on nextval() function of PostgreSQL.
You called next_by_code()
method of ir.sequence
object. In this method calls method _next()
. Let's see body:
def _next(self):
if self.sequence_id.implementation == 'standard':
number_next = _select_nextval(self.env.cr, 'ir_sequence_%03d_%03d' % (self.sequence_id.id, self.id))
else:
number_next = _update_nogap(self, self.sequence_id.number_increment)
return self.sequence_id.get_next_char(number_next)
The new value is generated using number_next. In your case _select_nextval
will be called:
def _select_nextval(cr, seq_name):
cr.execute("SELECT nextval('%s')" % seq_name)
return cr.fetchone()
As you can see function nextval
is called in query.
How it works.
OpenERP will call method _niu_validation
when you add some sale.order.line
in form. It is because object(each line) will calculate value for your field niu
. In your method it is called by next_by_code
. It means that before save(just for GUI / tree view) postgreSQL changed sequences. When you click on Save button(in header) the system calls _niu_validation
again.
To sum up.
Odoo doesn't apply changes in tree view until you click Save. You make some changes in tree view
and at the same time sequences will be changed in db(because you calculate value for column NIU
). But the lines are not saved in db.
You can check how it works here: path_to_odoo/openerp/addons/base/ir/ir_sequence.py
What can you do to make the sequence not changed?
You can remove method _niu_validation
, override method create
of sale.order.line
and calculate value for field when your line is stored.
But in this case user will not see new values for column 'on fly'.
Hope this helps you.
One more thing that is worth thinking about(if you want to use your approach). Imagine that several people are working on one order at the same time.
Upvotes: 2