Odoo使用api.constrains限制了产品的name,目的是让产品的名称唯一,但没想到的是使用复制的时候,会报错,说是已经存在的产品名称,去掉限制以后发现产品名称只有一个,复制出来的产品名称后会有一个副本的标识,可是为什么会报这样的一个错误呢?

经过一番研究,发现点击复制按钮以后,会调用两次contrains的check方法,一次使用的是产品名称(副本),第二次使用的是产品名称,然后使用search方法搜索,得到的结果居然是2个!

为什么会调用两次check方法呢?后来在父类的copy方法中找到了原因

def copy(self, cr, uid, id, default=None, context=None):
        """ copy(default=None)

        Duplicate record with given id updating it with default values

        :param dict default: dictionary of field values to override in the
               original values of the copied record, e.g: ``{'field_name': overridden_value, ...}``
        :returns: new record

        """
        if context is None:
            context = {}
        context = context.copy()
        data = self.copy_data(cr, uid, id, default, context)
        new_id = self.create(cr, uid, data, context)
        self.copy_translations(cr, uid, id, new_id, context)
        return new_id

Copy方法在创建了新的record以后,又调用了copy_translations方法,没猜错的话,问题应该出在这儿:

def copy_translations(self, cr, uid, old_id, new_id, context=None):
               ……………………
                for record in trans_obj.read(cr, uid, trans_ids, context=context):
                    del record['id']
                    # remove source to avoid triggering _set_src
                    del record['source']
                    record.update({'res_id': target_id})
                    if user_lang and user_lang == record['lang']:
                        # 'source' to force the call to _set_src
                        # 'value' needed if value is changed in copy(), want to see the new_value
                        record['source'] = old_record[field_name]
                        record['value'] = new_record[field_name]
                    trans_obj.create(cr, uid, record, context=context)

奇怪的是这里边并没有涉及到跟constrains有关的代码,倒是record比较令人怀疑。输出record会看到{'name':'product.template,name'}类似的值,但不清楚为什么translation会触发contrains.

跟踪错误代码提示,发现在low一级的_create方法中

 

for field in upd_todo:
            result += self._columns[field].set(cr, self, id_new, field, vals[field], user, rel_context) or []

 

 触发了write方法,而write方法又触发了_validate_fields从而导致两次验证

从传值来看,translation的source字段是原因,因此我们只需要在copy_translations中把old_id用新的代替,就能规避该问题。