Skip to Content
Menu
This question has been flagged
1007 Views

When create a new product with name "T-Shirt" and default_code "T-S001" and 2 Variants with Color "Black" and "White"
The variant will have default_code is "T-S001B" & "T-S001W"

I create 2 two function to set the attribute_value's first letter for the code of attributE.
And a function to take field code + product_attribute_value_id.code to have default_code for product.product
But, when i delete "Color" attribute the default_code of product disappeared

THis is my code:
from odoo import models, fields, api


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

default_code = fields.Char(compute='_compute_default_code', store=True)

@api.depends( 'product_tmpl_id', 'product_tmpl_id.default_code', 'product_template_attribute_value_ids', ) def _compute_default_code(self): for product in self: tmpl_default_code = '' if product.product_tmpl_id.default_code: tmpl_default_code = product.product_tmpl_id.default_code

attr_code = '' for rec in product.product_template_attribute_value_ids: attr_code += rec.product_attribute_value_id.code

product.default_code = tmpl_default_code + attr_code


from odoo import models, fields, api


class ProductAttributeValue(models.Model): _inherit = 'product.attribute.value'

code = fields.Char(string='Code', compute='_compute_code', store=True)

@api.depends('name', 'sequence') def _compute_code(self): for record in self: if record.name: # Lấy chữ cái đầu tiên của mỗi từ trong name code = ''.join(word[0].upper() for word in record.name.split()) record.code = code + str(record.sequence) # Thêm sequence vào mã để tránh chồng chéo else: record.code = ''

I just learned odoo recently
Sorry for my English and skill code


Avatar
Discard
Related Posts Replies Views Activity
4
Feb 25
14255
0
Jul 24
937
3
Mar 23
4559
0
Feb 22
2331
0
May 25
11