Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
4 Răspunsuri
20131 Vizualizări

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

Imagine profil
Abandonează
Cel mai bun răspuns

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.

Imagine profil
Abandonează
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 :)

Related Posts Răspunsuri Vizualizări Activitate
2
mai 23
7349
1
aug. 20
5917
2
apr. 20
2473
0
nov. 19
2372
0
ian. 21
2130