码迷,mamicode.com
首页 > 其他好文 > 详细

添加产品唯一性限制以后,复制按钮不能使用

时间:2015-06-04 13:21:10      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

使用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.

 

添加产品唯一性限制以后,复制按钮不能使用

标签:

原文地址:http://www.cnblogs.com/kfx2007/p/4551384.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!