Skip to Content
Menu
This question has been flagged
1 Reply
3346 Views

Hi everybody,

I'm trying to have the internal reference automatically set when creating new products.

Using automated actions, I used a python script that works just fine:


if not record.default_code:
seq = env['ir.sequence'].next_by_code('product.sequence')
record.write({'default_code' : seq})

The problem is that when creating variants for the product, that number is not passed to the variants. And as creating variants erases the reference of the main product, there isn't any reference anymore at all, nor for the main product, nor for the variants.

For now, I'd just like to have the reference number of the product copied to the variants when creating them.

How can it be?

Thanks

Avatar
Discard
Best Answer

Hello Virtual Crew,


As, "product.product" Inherits "product.template", so over-ride the create method of "product.product" to achieve this.


Add below code of snippet in you module's 'product_product.py' file.


//Code in Comment//


Hope this helps.

   

If you need any help in customization feel free to contact us.


Thanks & Regards,

Email:  odoo@aktivsoftware.com           

Skype: kalpeshmaheshwari

Avatar
Discard

Code :

from odoo import models, api

class ProductProduct(models.Model):
_inherit = 'product.product'

@api.model_create_multi
def create(self, vals_list):
# Call the original create method
products = super(ProductProduct, self).create(vals_list)

for product in products:
# Check if the product template has a default code and the variant doesn't
if product.product_tmpl_id.default_code and not product.default_code:
# Set the variant's default_code to the product template's default_code
product.default_code = product.product_tmpl_id.default_code

return products

Related Posts Replies Views Activity
1
Jan 23
1473
1
Dec 16
4362
2
Aug 25
543
0
Jul 25
474
1
Jul 25
907