我在OpenERP/PostgreSQL中有一个表,其中包含以下列:name和description.
我为唯一名称添加了以下验证:
_sql_constraints = [('unique_name', 'unique(name)', 'A record with the same name already exists.')]
Run Code Online (Sandbox Code Playgroud)
它工作正常,但它区分大小写.目前,它接受诸如"Mickey","MICKEY"和"mickey"之类的值:
Wrong Way:
--------------------------
| name | description |
--------------------------
| mickey | not a mouse |
--------------------------
| MICKEY | not a mouse |
--------------------------
| Mickey | not a mouse |
--------------------------
Run Code Online (Sandbox Code Playgroud)
有没有办法修改验证代码,以便它不允许用户添加几个值,如"米奇","MICKEY"和"米奇"?如何使唯一密钥验证不敏感?
Right Way:
--------------------------------
| name | description |
--------------------------------
| mickey | not a mouse |
--------------------------------
| mickey mouse | is a mouse |
--------------------------------
| donald | is a duck |
--------------------------------
Run Code Online (Sandbox Code Playgroud)
Ruc*_*kla 15
要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'])]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7231 次 |
| 最近记录: |