Skip to Content
Menu
This question has been flagged
5 Replies
2488 Views

class ProductAutoBarcode(models.Model): 

 _inherit = 'product.product'

@api.model 

def create(self, vals): 

res = super(ProductAutoBarcode, self).create(vals) 

ean = generate_ean(str(res.id))
res.barcode = ean 

 return res

note

on print its give me value on every time when product created --------------------->ean 

1 - this code is not working on first time when i create product with variants

when it work.

when i create product without variants and save button press then its giving me value else empty  


Error

Code

Avatar
Discard
Best Answer

Hi Usman,

To generate barcode, qrcode or any code in odoo, odoo provides default feature to do this.

There is one controller which will generate barcode. So what you need to do is, you need to call that controller from your create method.

Just search, controller "/report/barcode"


I hope it will useful for you.


Regards

Haresh Kansara



Avatar
Discard
Best Answer


class ProductAutoBarcode(models.Model): 

 _inherit = 'product.product'

 @api.model 

 def create(self, vals): 

  ean = generate_ean(str(res.id))

  vals.update({'barcode': ean})

  return super(ProductAutoBarcode, self).create(vals) 

Avatar
Discard
Author

not working same result ...

Which version you are using?

Can you give me generate_ean method also?

Author

v 13

def generate_ean(ean):

"""Creates and returns a valid ean13 from an invalid one"""

if not ean:

return

ean = re.sub("[A-Za-z]", "0", ean)

ean = re.sub("[^0-9]", "", ean)

ean = ean[:13]

if len(ean) < 13:

ean = ean + '0' * (13 - len(ean))

return ean[:-1] + str(ean_checksum(ean))

Related Posts Replies Views Activity
0
Apr 24
562
0
Dec 19
3496
0
Aug 19
4
5
Aug 19
1783
1
Feb 19
5689