OpenERP 7 _constraints

时间:2013-04-17 05:54:06

标签: python xml openerp

class deduction_registration(osv.osv):
    _name = "bpl.deduction.registration"
    _description = "Deduction Registration"
    _columns = {
        'name': fields.char('Deduction Name', size=256, required=True),
    }
    _sql_constraints = [('deduction_registration_name_unique', 'unique(name)', 'Deduction name already exists')]

deduction_registration()

我为我的模型创建了上面的sql约束。但它仍然允许没有区分大小写的重复记录。

允许 EPF epf 如何为此添加约束。?

1 个答案:

答案 0 :(得分:2)

class deduction_registration(osv.osv):

    def _check_unique_insesitive(self, cr, uid, ids, context=None):
        sr_ids = self.search(cr, 1 , [], context=context)
        lst = [x.name.lower() for x in self.browse(cr, uid, sr_ids, context=context) if x.name and x.id not in ids]
        for self_obj in self.browse(cr, uid, ids, context=context):
            if self_obj.name and self_obj.name.lower() in  lst:
                return False
            return True

    _name = "bpl.deduction.registration"
    _description = "Deduction Registration"
    _columns = {
        'name': fields.char('Deduction Name', size=256, required=True),
    }
    _sql_constraints = [('deduction_registration_name_unique', 'unique(name)', 'Deduction name already exists')]
    _constraints = [(_check_unique_insesitive, 'Deduction name already exists', ['name'])]

deduction_registration()

用这个代码完成了我的工作: - )