Ir al contenido
Menú
Se marcó esta pregunta
4 Respuestas
19902 Vistas

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
Descartar
Mejor respuesta

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
Descartar
Autor

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 :)

Publicaciones relacionadas Respuestas Vistas Actividad
2
may 23
6934
1
ago 20
5709
2
abr 20
2270
0
nov 19
2247
0
ene 21
2032