Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
4 ตอบกลับ
20231 มุมมอง

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

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

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.

อวตาร
ละทิ้ง
ผู้เขียน

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 ตอบกลับ มุมมอง กิจกรรม
Can't create view (model not found) แก้ไขแล้ว
2
พ.ค. 23
7681
1
ส.ค. 20
6011
2
เม.ย. 20
2550
0
พ.ย. 19
2428
0
ม.ค. 21
2193