Se rendre au contenu
Menu
Cette question a été signalée
5 Réponses
3738 Vues

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
Ignorer
Meilleure réponse

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
Ignorer
Meilleure réponse


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
Ignorer
Auteur

not working same result ...

Which version you are using?

Can you give me generate_ean method also?

Auteur

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

Publications associées Réponses Vues Activité
0
avr. 24
1939
0
déc. 19
4635
0
août 19
4
5
août 19
2806
1
févr. 19
6976