Skip to content

Commit 49c3ca7

Browse files
committed
[FIX] product_attribute_set: auto update attributes by updating attribute_set_id
Adding some test cases to confirm that the attributes will only be visible if the attribute set id has a value.
1 parent 09f0d94 commit 49c3ca7

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

product_attribute_set/demo/product_attribute.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,11 @@
6565
<field name="field_id" eval="ref('product.field_product_template__weight')" />
6666
<field name="attribute_group_id" ref="computer_transport_attribute_group" />
6767
<field name="attribute_set_ids" eval="[(4, ref('computer_attribute_set'))]" />
68-
<field name="model_id" ref="product.model_product_template" />
6968
</record>
7069
<record id="computer_volume_attribute" model="attribute.attribute">
7170
<field name="nature">native</field>
7271
<field name="field_id" eval="ref('product.field_product_template__volume')" />
7372
<field name="attribute_group_id" ref="computer_transport_attribute_group" />
7473
<field name="attribute_set_ids" eval="[(4, ref('computer_attribute_set'))]" />
75-
<field name="model_id" ref="product.model_product_template" />
7674
</record>
7775
</odoo>

product_attribute_set/models/product.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ def _onchange_categ_id(self):
4747
if self.categ_id and not self.attribute_set_id:
4848
self.attribute_set_id = self.categ_id.attribute_set_id
4949

50+
@api.onchange("attribute_set_id")
51+
def _onchange_attribute_set_id(self):
52+
"""Trigger _get_view_fields implicitly to auto update attributes in the UI"""
53+
if self.attribute_set_id:
54+
view_id = self.env.ref("product.product_template_only_form_view").id
55+
view = self.get_views([(view_id, "form")], {})
56+
view.get("models")
57+
5058

5159
class ProductProduct(models.Model):
5260

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import test_product
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright 2025 Kencove (http://kencove.com).
2+
# @author Mohamed Alkobrosli <[email protected]>
3+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
4+
5+
import lxml.etree as ET
6+
7+
from odoo.tests.common import Form, TransactionCase
8+
9+
10+
class TestProductAttributeSet(TransactionCase):
11+
def setUp(self):
12+
self.ProductTemplate = self.env["product.template"]
13+
self.view_id = self.env.ref("product.product_template_only_form_view").id
14+
self.computer = self.env.ref("product_attribute_set.computer_attribute_set")
15+
self.x_technical_description = self.env.ref(
16+
"product_attribute_set.computer_tech_description_attribute"
17+
)
18+
super().setUp()
19+
20+
def test_onchange_attribute_set_id(self):
21+
"""Test that onchange on attribute_set_id is triggered and view fields are updated"""
22+
23+
product = None
24+
25+
with self.assertRaises(AssertionError):
26+
# AssertionError: can't write on invisible field x_technical_description
27+
with Form(self.ProductTemplate) as f:
28+
f.name = "Test Product"
29+
f.x_technical_description = "high quality product"
30+
f.save()
31+
with Form(self.ProductTemplate) as f:
32+
f.name = "Test Product"
33+
f.attribute_set_id = self.computer
34+
f.x_technical_description = "high quality product"
35+
product = f.save()
36+
# Asserting the field is only writeable when it is visible
37+
self.assertEqual(
38+
product.x_technical_description,
39+
"high quality product",
40+
)
41+
42+
views = product.get_views([(self.view_id, "form")], {})
43+
arch = views["views"]["form"]["arch"]
44+
xml_arch = ET.fromstring(arch)
45+
field_node = xml_arch.xpath("//field[@name='x_technical_description']")
46+
self.assertTrue(field_node)
47+
field_node = field_node[0]
48+
attrs = field_node.get("attrs")
49+
# This domain as it is rendered with attributes,
50+
# it will make them visible only if the attribute set id equals 1
51+
self.assertEqual(attrs, "{'invisible': [('attribute_set_id', 'not in', [1])]}")
52+
self.assertEqual(self.computer.id, 1)

0 commit comments

Comments
 (0)