Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD] product_compute_template_field_from_variant_helper module #1840

Open
wants to merge 1 commit into
base: 16.0
Choose a base branch
from

Conversation

legalsylvain
Copy link
Contributor

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

Develop

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

Roadmap

Do not port this module in version 17.0.

(the feature is native since V17.0)

@legalsylvain legalsylvain added this to the 16.0 milestone Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant