İçereği Atla
Menü
This question has been flagged
4 Cevaplar
19748 Görünümler

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
Vazgeç
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
Vazgeç
Üretici

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 Cevaplar Görünümler Aktivite
2
May 23
6702
1
Ağu 20
5562
2
Nis 20
2127
0
Kas 19
2131
0
Oca 21
1940