【问题标题】:OpenERP Unique ConstraintOpenERP 唯一约束
【发布时间】:2012-10-27 03:24:54
【问题描述】:

我在 OpenERP/PostgreSQL 中有一个包含以下列的表:namedescription

我为唯一名称添加了以下验证:

_sql_constraints = [('unique_name', 'unique(name)', 'A record with the same name already exists.')]

它工作正常,但区分大小写。目前,它接受“Mickey”、“MICKEY”和“mickey”等值:

Wrong Way:
--------------------------
| name   | description   |
--------------------------
| mickey | not a mouse   |
--------------------------
| MICKEY | not a mouse   |
--------------------------
| Mickey | not a mouse   |
--------------------------

有没有办法修改验证码,使其不允许用户添加多个值,例如“Mickey”、“MICKEY”和“mickey”?如何使唯一密钥验证不区分大小写?

Right Way:
--------------------------------
| name         | description   |
--------------------------------
| mickey       | not a mouse   |
--------------------------------
| mickey mouse | is a mouse    |
--------------------------------
| donald       | is a duck     |
--------------------------------

【问题讨论】:

    标签: python openerp


    【解决方案1】:

    这种方式无需从数据库中读取所有数据:

    def _check_unique_insesitive(self, cr, uid, ids, context=None):
    
        for self_obj in self.browse(cr, uid, ids, context=context):
            if self_obj.name and self.search_count(cr, uid, [('name', '=ilike', self_obj.name), ('id', '!=', self_obj.id)], context=context) != 0:
                return False
    
        return True
    
    _constraints = [(_check_unique_insesitive, _('The name must be unique!'), ['name'])]
    

    【讨论】:

      【解决方案2】:

      在 Odoo 8.0 或更高版本中以更简单的方式使用约束。 获取模型的所有记录并使用lower()检查所需的字段值并排除自我记录。

      @api.constrains('code')
      def _check_duplicate_code(self):
          codes = self.search([])
              for c in codes:
                  if self.code.lower() == c.code.lower() and self.id != c.id:
                      raise exceptions.ValidationError("Error: code must be unique")
      

      【讨论】:

      • 答案应该包含对哪些代码的描述,而不仅仅是一段代码。一年多以前的问题以及其他答案的答案,尤其应包括由于语言的任何变化或其他已发布答案之间的差异而导致该答案相关的原因。
      【解决方案3】:

      对于case insensitive constraints,请查看HERE 否则你总是可以使用 Openerp Constraints 而不是 SQL 。

      对于 openerp 约束

      查看示例

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

      【讨论】:

      • 嗨鲁奇!感谢您的及时回复。你能给我一个例子,我可以如何使用 OpenERP 约束来实现所需的验证吗?
      • 在一个列表中列出特定字段的所有数据。将其转换为小写或大写,然后使用 in 运算符检查新值,并返回与结果相反的值。
      • 成功了。谢谢鲁奇!我刚刚修改了列表以排除最后一个条目
      • 你的意思是当前对象的数据?如果您有不同的排序顺序,那么最后一个条目将不会总是起作用,请将此 lst = [x.FIELD.lower() for x in self.browse(cr, uid, sr_ids, context=context) if x.FIELD] 更改为lst = [x.FIELD.lower() for x in self.browse(cr, uid, sr_ids, context=context) if x.FIELD and if x.id not in ids]
      • Yes Ruchir,表格中输入/输入的数据。我这样做了: lst = [x.name.lower() for x in self.browse(cr, uid, sr_ids, context=context) if x.name and x.id !=last_rec_id] 感谢您的提示
      猜你喜欢
      • 2013-04-11
      • 2015-03-06
      • 2019-09-25
      • 2021-01-22
      • 2015-06-07
      • 2011-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多