← Back to team overview

openerp-community team mailing list archive

lp:~openerp-community/openerp-product-attributes/7.0-product_custom_attributes-allow-to-bind-on-existing-field into lp:openerp-product-attributes

 

Guewen Baconnier @ Camptocamp has proposed merging lp:~openerp-community/openerp-product-attributes/7.0-product_custom_attributes-allow-to-bind-on-existing-field into lp:openerp-product-attributes.

Commit message:
[IMP] allow to create an attribute on an existing (and not manual) field.

Formerly, when we give a field_id, the following error would be raised:
"Properties of base fields cannot be altered in this manner! Please modify them through Python code, preferably through a custom addon"

Requested reviews:
  Product Core Editors (product-core-editors)

For more details, see:
https://code.launchpad.net/~openerp-community/openerp-product-attributes/7.0-product_custom_attributes-allow-to-bind-on-existing-field/+merge/190146

I propose this patch to allow to create an attribute.attribute on a ir.model.fields which is not manual and already exists.
The attribute_attribute.create() method actually adds values which are send to ir_model_fields.write() and this one raises an error.

My proposal just skip the definition of the ir.model.fields values when the field already exists and is not manual.

The use case for the change is when fields are created in a module, but you want to have them displayed only in some attribute sets,
and you want them displayed on the same form than the other attributes.
-- 
https://code.launchpad.net/~openerp-community/openerp-product-attributes/7.0-product_custom_attributes-allow-to-bind-on-existing-field/+merge/190146
Your team OpenERP Community is subscribed to branch lp:openerp-product-attributes.
=== modified file 'base_custom_attributes/custom_attributes.py'
--- base_custom_attributes/custom_attributes.py	2013-09-27 19:24:53 +0000
+++ base_custom_attributes/custom_attributes.py	2013-10-09 14:22:50 +0000
@@ -179,6 +179,21 @@
         }
 
     def create(self, cr, uid, vals, context=None):
+        if vals.get('field_id'):
+            field_obj = self.pool.get('ir.model.fields')
+            field = field_obj.browse(cr, uid, vals['field_id'], context=context)
+            if vals.get('serialized'):
+                raise orm.except_orm(
+                    _('Error'),
+                    _("Can't create a serialized attribute on "
+                      "an existing ir.model.fields (%s)") % field.name)
+            if field.state != 'manual':
+                # the ir.model.fields already exists and we want to map
+                # an attribute on it. We can't change the field so we
+                # won't add the ttype, relation and so on.
+                return super(attribute_attribute, self).create(cr, uid, vals,
+                                                               context=context)
+
         if vals.get('relation_model_id'):
             relation = self.pool.get('ir.model').read(cr, uid,
             [vals.get('relation_model_id')], ['model'])[0]['model']


Follow ups