Skip to Content
Menu
v 16.0 Third Party
Download for v 16.0 Deploy on Odoo.sh
Availability
Odoo Online
Odoo.sh
On Premise
Odoo Apps Dependencies Discuss (mail)
Lines of code 27
Technical Name product_compute_template_field_from_variant_helper
LicenseLGPL-3
Websitehttps://github.com/OCA/product-attribute
You bought this module and need support? Click here!
Odoo Community Association

Products - Compute Technical Fields (template from Variant)

Beta License: LGPL-3 OCA/product-attribute Translate me on Weblate Try me on Runboat

This module extends the functionality of product module, introducing a technical helper to define easily template fields that are computed from variant values.

Table of contents

Development

Replace this code:

class ProductTemplate(models.Model):
    _inherit = "product.template"

    net_weight = fields.Float(
        compute="_compute_net_weight",
        inverse="_inverse_net_weight",
        store=True,
    )

    @api.depends("product_variant_ids", "product_variant_ids.net_weight")
    def _compute_net_weight(self):
        for template in self:
            if template.product_variant_count == 1:
                template.net_weight = template.product_variant_ids.net_weight
            else:
                template.net_weight = 0.0

    def _inverse_net_weight(self):
        for template in self:
            if len(template.product_variant_ids) == 1:
                template.product_variant_ids.net_weight = template.net_weight

  def _get_related_fields_variant_template(self):
      res = super()._get_related_fields_variant_template()
      res.append("net_weight")
      return res

By this code:

class ProductTemplate(models.Model):
    _inherit = "product.template"

    net_weight = fields.Float(
        compute=lambda x: x._compute_template_field_from_variant_field("net_weight"),
        inverse=lambda x: x._set_product_variant_field("net_weight"),
        store=True,
    )

  def _get_related_fields_variant_template(self):
      res = super()._get_related_fields_variant_template()
      res.append("net_weight")
      return res

Known issues / Roadmap

Do not port this module in version 17.0.

(the feature is native since V17.0)

Bug Tracker

Bugs are tracked on GitHub Issues. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us to smash it by providing a detailed and welcomed feedback.

Do not contact contributors directly about support or help with technical issues.

Credits

Authors

  • Odoo SA
  • GRAP

Contributors

  • Sylvain LE GAL (https://www.twitter.com/legalsylvain)
  • Odoo SA.

The code of this module is a backport of the code of product module in Odoo Version 18.0 CE.

https://github.com/odoo/odoo/blob/fa0c3d83b7d3bb06d2a23525f4334671574da358/addons/product/models/product_template.py#L264-L303

Maintainers

This module is maintained by the OCA.

Odoo Community Association

OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use.

Current maintainer:

legalsylvain

This module is part of the OCA/product-attribute project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

Please log in to comment on this module

  • The author can leave a single reply to each comment.
  • This section is meant to ask simple questions or leave a rating. Every report of a problem experienced while using the module should be addressed to the author directly (refer to the following point).
  • If you want to start a discussion with the author, please use the developer contact information. They can usually be found in the description.
Please choose a rating from 1 to 5 for this module.