odoo重复公司得到错误

时间:2015-09-03 15:39:34

标签: odoo-8

我覆盖了res.company的副本和创建函数,如下所示。

def copy(self, cr, uid, id, default=None, context=None):
        self.duplicate = True
        old_rc = self.browse(cr, uid, [id], context=context)[0]

        company_name = self.read(cr, uid, [id], ['name'])[0]['name']
        default.update({'name': _('%s (copy)')% company_name})
        if old_rc.custom_company_code:
            default.update({'custom_company_code': old_rc.custom_company_code + "-Copy"})
        else:
            default.update({'custom_company_code': old_rc.name + "-Copy"})
        return super(ResCompany, self).copy(cr, uid, id, default, context=context)

def create(self, cr, uid, vals, context=None):
        if vals.get('name', False):
            temp_name = vals.get('name', False)
            temp_name = temp_name.replace('\'', '"')
            cr.execute("SELECT id FROM res_company WHERE lower(name)=lower(E'" + temp_name + "')")
            uniq_comps = set(id[0] for id in cr.fetchall())
            if len(uniq_comps) > 0:
                raise UserError(_("Name must be unique for company '%s'.") % (vals.get('name', False),))

        if not vals.get('custom_company_code', False):
            vals['custom_company_code'] = self.pool.get('ir.sequence').next_by_code(cr, uid, 'res_company_code_sequence') or '/'

        if vals.get('custom_company_code', False):
            vals['custom_company_code'] = vals.get('custom_company_code', '').upper()
        if not vals.get('name', False) or vals.get('partner_id', False):
            self.cache_restart(cr)
        return super(ResCompany, self).create(cr, uid, vals, context=context)

custom_company_code是user_requirement。但是当我点击"复制"在"更多",我得到了

  

Odoo警告 - 验证错误

     

公司名称必须是唯一的!

当我检查日志时,我在弹出上述错误之前得到了以下行

  

openerp.sql_db:错误查询:UPDATE" res_company" SET" name" ='我的   公司' WHERE id = 4

我如何解决问题!!

1 个答案:

答案 0 :(得分:0)

解决了这个问题。因为partner_id

default.update({' partner_id':False})

例如

def copy(self, cr, uid, id, default=None, context=None):
        company_name = self.read(cr, uid, [id], ['name'])[0]['name']
        default.update({'name': _('%s-Copy')% company_name})
        default.update({'partner_id': False})
        return super(res_company, self).copy(cr, uid, id, default, context=context)