Skip to Content
Menu
This question has been flagged
4 Replies
19097 Views

Hi guys;

My sequence increase the value when I save or cancel the process using this field.

name = fields.Char(string='Purchase Name', 
default=lambda self: self.env['ir.sequence'].next_by_code('tjara.purchase_order.seq'))

I updated my code to the following but it pop up an error. Also this code don't display the name before saving, the name's field show only a blank espace :

@api.model
def create(self, vals):
vals['name'] = self.env['ir.sequence'].next_by_code('tjara.purchase_order.seq')
return super(purchase_order, self).create(vals)

I want to :

- Genrate the name using a sequence.

- Display it even before saving the record.

- When the user cancel and reopen the form the sequence generate the same name.

Thanks a lot :)

Avatar
Discard
Best Answer

Hi,

This code return the next reference without increment

sequence = self.env['ir.sequence'].search([('code','=','sequence_code')])
next= sequence.get_next_char(sequence.number_next_actual)

So, you can make an on-change method and use the next value to display it with form opening and call next_by_code only on create.

PS: Next number that will be displayed can be incremented due to parallel object create. So the value might already be obsolete. I think you have to handle this case. 

Best regards.

Avatar
Discard
Author

Thank you Souheil, you helped me with some useful functions.

I haven't noticed the existence of such methods or I didn't find some clear examples about each one.

This is what I added in the purchase_order.py to get what I want :

name = fields.Char(string='Purchase Name', default=lambda self: self._get_next_purchasename(), store=True, readonly=True)

@api.model

def _get_next_purchasename(self):

sequence = self.env['ir.sequence'].search([('code','=','tjara.purchase_order.seq')])

next= sequence.get_next_char(sequence.number_next_actual)

return next

@api.model

def create(self, vals):

vals['name'] = self.env['ir.sequence'].next_by_code('tjara.purchase_order.seq')

result = super(purchase_order, self).create(vals)

return result

Always happy to help :)

Related Posts Replies Views Activity
2
May 23
5651
1
Aug 20
4998
2
Apr 20
1842
0
Nov 19
1874
0
Jan 21
1616