From d6daf5fa2f3960d40b0fdf7e93365d918dade565 Mon Sep 17 00:00:00 2001
From: Cedric Snauwaert <csn@openerp.com>
Date: Fri, 12 Apr 2013 15:08:56 +0200
Subject: [PATCH] [FIX] product: add store=dict to field name_template

Recompute the name_template field if the template changes.

backport of 41628fc + f25c5d8 to 7.0 plus some optimisations in triggers
Fixes #3277
---
 addons/product/product.py | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/addons/product/product.py b/addons/product/product.py
index 69dc23b3fb6a..98a33a98f8a4 100644
--- a/addons/product/product.py
+++ b/addons/product/product.py
@@ -530,6 +530,10 @@ class product_product(osv.osv):
     def _set_image(self, cr, uid, id, name, value, args, context=None):
         return self.write(cr, uid, [id], {'image': tools.image_resize_image_big(value)}, context=context)
 
+    def _get_name_template_ids(self, cr, uid, ids, context=None):
+        template_ids = self.pool.get('product.product').search(cr, uid, [('product_tmpl_id', 'in', ids)])
+        return list(set(template_ids))
+
     _defaults = {
         'active': lambda *a: 1,
         'price_extra': lambda *a: 0.0,
@@ -561,7 +565,11 @@ class product_product(osv.osv):
         'price_extra': fields.float('Variant Price Extra', digits_compute=dp.get_precision('Product Price')),
         'price_margin': fields.float('Variant Price Margin', digits_compute=dp.get_precision('Product Price')),
         'pricelist_id': fields.dummy(string='Pricelist', relation='product.pricelist', type='many2one'),
-        'name_template': fields.related('product_tmpl_id', 'name', string="Template Name", type='char', size=128, store=True, select=True),
+        'name_template': fields.related('product_tmpl_id', 'name', string="Template Name", type='char', size=128, store={
+            'product.template': (_get_name_template_ids, ['name'], 10),
+            'product.product': (lambda self, cr, uid, ids, c=None: ids, ['product_tmpl_id'], 10),
+
+            }, select=True),
         'color': fields.integer('Color Index'),
         # image: all image fields are base64 encoded and PIL-supported
         'image': fields.binary("Image",
-- 
GitLab