diff --git a/attribute_set/README.rst b/attribute_set/README.rst index 2cac28a72..d9563f20b 100644 --- a/attribute_set/README.rst +++ b/attribute_set/README.rst @@ -90,10 +90,10 @@ Attributes : 2. You need to **add a placeholder** ```` at the desired location in the model's form view. -3. Finally, **add a context** ``{"include_native_attribute": True}`` on - the action leading to this form view if the model's view needs to - display attributes related to native fields together with the other - "custom" attributes. +3. Finally, **add a context** + ``{"include_native_attribute_view_ref": True}`` on the action leading + to this form view if the model's view needs to display attributes + related to native fields together with the other "custom" attributes. Known issues / Roadmap ====================== diff --git a/attribute_set/models/__init__.py b/attribute_set/models/__init__.py index a4d368306..12810ab94 100644 --- a/attribute_set/models/__init__.py +++ b/attribute_set/models/__init__.py @@ -3,3 +3,4 @@ from . import attribute_set from . import attribute_set_owner from . import attribute_group +from . import many2one_override diff --git a/attribute_set/models/attribute_set_owner.py b/attribute_set/models/attribute_set_owner.py index 9624442a1..2dba40a2c 100644 --- a/attribute_set/models/attribute_set_owner.py +++ b/attribute_set/models/attribute_set_owner.py @@ -33,7 +33,7 @@ def _build_attribute_eview(self): ("model", "=", self._name), ("attribute_set_ids", "!=", False), ] - if not self._context.get("include_native_attribute"): + if not self._context.get("include_native_attribute_view_ref"): domain.append(("nature", "=", "custom")) attributes = self.env["attribute.attribute"].search(domain) @@ -71,7 +71,7 @@ def _insert_attribute(self, arch): ) ) - if self._context.get("include_native_attribute"): + if self._context.get("include_native_attribute_view_ref"): self.remove_native_fields(eview) attribute_eview = self._build_attribute_eview() diff --git a/attribute_set/models/many2one_override.py b/attribute_set/models/many2one_override.py new file mode 100644 index 000000000..b92ccf2d6 --- /dev/null +++ b/attribute_set/models/many2one_override.py @@ -0,0 +1,22 @@ +from odoo.fields import Many2one +from odoo.exceptions import MissingError + +# Override the Many2one field +# class Many2oneOverride(fields.Many2one): + +def patch_convert_to_read(self, value, record, use_display_name=True): + if use_display_name and value: + try: + return (value.id, value.sudo().display_name) + except MissingError: + return False + elif value: + return value.id + # In general odoo assumes use_display_name is True if value is empty + # If use_display_name is False is not in account or if value is empty + # This fix will have a PR in odoo repositoy, + # and if approved this fix code will be removed + else: + return False + +Many2one.convert_to_read = patch_convert_to_read diff --git a/attribute_set/readme/USAGE.md b/attribute_set/readme/USAGE.md index 0066c2733..82a8bb2dd 100644 --- a/attribute_set/readme/USAGE.md +++ b/attribute_set/readme/USAGE.md @@ -1,15 +1,15 @@ Even if this module does not provide views to display some model's -Attributes, it provides however a Technical menu in *Settings \> -Technical \> Database Structure \> Attributes* to **create new +Attributes, it provides however a Technical menu in _Settings \> +Technical \> Database Structure \> Attributes_ to **create new Attributes**. An Attribute is related to both an Attribute Group and an Attribute Set : -- The **Attribute Set** is related to the *"model's category"*, i.e. all +- The **Attribute Set** is related to the _"model's category"_, i.e. all the model's instances which will display the same Attributes. -- The **Attribute Group** is related to the *"attribute's category"*. +- The **Attribute Group** is related to the _"attribute's category"_. All the attributes from the same Attribute Set and Attribute Group will be displayed under the same field's Group in the model's view. @@ -18,7 +18,7 @@ An Attribute is related to both an Attribute Group and an Attribute Set > Setting **"Advanced Attribute Set settings"** > (`group_advanced_attribute_set`). ------------------------------------------------------------------------- +--- If you want to create a module displaying some specific model's Attributes : @@ -28,7 +28,7 @@ Attributes : 2. You need to **add a placeholder** `` at the desired location in the model's form view. -3. Finally, **add a context** `{"include_native_attribute": True}` on +3. Finally, **add a context** `{"include_native_attribute_view_ref": True}` on the action leading to this form view if the model's view needs to display attributes related to native fields together with the other "custom" attributes. diff --git a/attribute_set/static/description/index.html b/attribute_set/static/description/index.html index 5612f080c..60fc94ebd 100644 --- a/attribute_set/static/description/index.html +++ b/attribute_set/static/description/index.html @@ -435,10 +435,10 @@

Usage

  • You need to add a placeholder <separator name="attributes_placeholder" /> at the desired location in the model’s form view.
  • -
  • Finally, add a context {"include_native_attribute": True} on -the action leading to this form view if the model’s view needs to -display attributes related to native fields together with the other -“custom” attributes.
  • +
  • Finally, add a context +{"include_native_attribute_view_ref": True} on the action leading +to this form view if the model’s view needs to display attributes +related to native fields together with the other “custom” attributes.
  • diff --git a/attribute_set/tests/test_build_view.py b/attribute_set/tests/test_build_view.py index 3015032da..c5ff7a6df 100644 --- a/attribute_set/tests/test_build_view.py +++ b/attribute_set/tests/test_build_view.py @@ -287,10 +287,12 @@ def test_render_all_field_type(self): self.assertFalse(attr.get("nolabel", False)) # TEST on NATIVE ATTRIBUTES - def _get_eview_from_get_views(self, include_native_attribute=True): + def _get_eview_from_get_views(self, include_native_attribute_view_ref=True): result = ( self.env["res.partner"] - .with_context(include_native_attribute=include_native_attribute) + .with_context( + include_native_attribute_view_ref=include_native_attribute_view_ref + ) .get_views([(self.view.id, "form")]) ) return etree.fromstring(result["views"]["form"]["arch"]) @@ -314,8 +316,8 @@ def test_native_readonly(self): self.assertTrue(attr[0].get("readonly")) def test_no_include_native_attr(self): - # Run get_views on the test view with no "include_native_attribute" - eview = self._get_eview_from_get_views(include_native_attribute=False) + # Run get_views on the test view with no "include_native_attribute_view_ref" + eview = self._get_eview_from_get_views(include_native_attribute_view_ref=False) attr = eview.xpath(f"//field[@name='{self.attr_native.name}']") # Only one field with this name diff --git a/attribute_set/wizard/attribute_option_wizard.py b/attribute_set/wizard/attribute_option_wizard.py index 127703890..87129cb90 100644 --- a/attribute_set/wizard/attribute_option_wizard.py +++ b/attribute_set/wizard/attribute_option_wizard.py @@ -19,7 +19,9 @@ class AttributeOptionWizard(models.TransientModel): "attribute.attribute", "Product Attribute", required=True, - default=lambda self: self.env.context.get("attribute_id", False), + # if the context is passed in correctly by suffixing it with _view_ref + # we will no longer need default_get method to assign attribute_id a value + default=lambda self: self.env.context.get("attribute_id_view_ref", False), ondelete="cascade", ) option_ids = fields.One2many( @@ -52,15 +54,6 @@ def create(self, vals_list): del vals["option_ids"] return super().create(vals_list) - @api.model - def default_get(self, fields_list): - res = super().default_get(fields_list) - # Retrieve attribute_id_view_ref from context - context = self.env.context - if context.get("attribute_id_view_ref"): - res["attribute_id"] = context.get("attribute_id_view_ref") - return res - @api.model def get_views(self, views, options=None): context = self.env.context