0
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 约束。但它仍然允许不区分大小写的重复记录。

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

4

1 回答 1

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()

有了这段代码,我已经完成了我的工作:-)

于 2013-04-17T06:00:28.397 回答