如何在带有postgres的Django中使用不区分重音的过滤器? [英] how to have Accent-insensitive filter in django with postgres?
问题描述
我发现在postgres数据库上,我们无法(在旧邮件交换中)配置默认的重音敏感度。
是否也可以使用_icontains对特殊角色不敏感(é,è,à,ç,ï),还是我必须使用postgres正则表达式将双方替换为_iregex(ç-> c,é-> e ...)?
编辑:
这个问题是古老的,在1.8之前为django的用户保留。对于使用最新django版本的用户,请使用以下新方法: https://docs.djangoproject.com/zh-CN/dev/ref/contrib/postgres/lookups/#std:fieldlookup-unaccent
编辑:Django 1.8对内置的postgresql进行了重音不敏感的查找。 https://docs.djangoproject.com/ zh_cn / dev / ref / contrib / postgres / lookups /#std:fieldlookup-unaccent
事实上,在postgres contrib(8.4+)中,易于搜索的功能:
for postgres 9 / 8.5:
- https://github.com/adunstan/postgresql-dev/commits/master/contrib / unaccent
- http:/ /www.sai.msu.su/~megera/wiki/unaccent 对于postgres 8.4,
此处是django用法的示例:
vals = MyObject.object s.raw(
SELECT * \
from myapp_myobject \
WHERE unaccent(name)LIKE \'% + search_text +%')
在比较之前,您可以在文本搜索中应用不重音。
选项我所做的是:
#!/ usr / bin / env python
#-*-编码:utf-8 -*-
#部分信用来自django.db.backends.postgresql_psycopg2.base import * clarisys.fr
*
class DatabaseOperations(DatabaseOperations):
def lookup_cast(self,lookup_type):
如果lookup_type in('icontains','istartswith'):
return UPPER(unaccent(%s :: text))
else:
return super(DatabaseOperations,self).lookup_cast(lookup_type)
class DatabaseWrapper(DatabaseWrapper):
def __init __(self,* args,** kwargs):
super(DatabaseWrapper,self).__ init __(* args,** kwargs)
self.operators ['icontains'] ='LIKE UPPER(unaccent(%s)) '
self.operators ['istartswith'] ='喜欢大写(unaccent(%s))'
self.ops = DatabaseOperations(self)
在文件夹中使用此文件 base.py
并将此文件夹用作数据库后端。 icontains和istartswith现在不区分大小写和变音。
Hi I find that on postgres database, we can't configure default accent sensivity (on old mail exchanges).
Is there a way to have a _icontains also insensitive to special caracters (é, è, à, ç, ï) or I must use postgres regex to replace both side with _iregex (ç->c, é->e ...)?
edit: this question is old, and is kept for users of django before 1.8. For those using latest django versions, here the new way: https://docs.djangoproject.com/en/dev/ref/contrib/postgres/lookups/#std:fieldlookup-unaccent
EDIT: Django 1.8 makes accent unsensitive lookup for postgresql builtin. https://docs.djangoproject.com/en/dev/ref/contrib/postgres/lookups/#std:fieldlookup-unaccent
In fact in postgres contrib (8.4+) there is an unaccent function to search easily:
for postgres 9/8.5:
- https://github.com/adunstan/postgresql-dev/commits/master/contrib/unaccent
- http://www.sai.msu.su/~megera/wiki/unaccent
for postgres 8.4:
here an example of usage from django:
vals = MyObject.objects.raw(
"SELECT * \
FROM myapp_myobject \
WHERE unaccent(name) LIKE \'%"+search_text+"%'")
You may apply apply unaccent on text-search before comparison.
Option I made is:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# parts of credits comes to clarisys.fr
from django.db.backends.postgresql_psycopg2.base import *
class DatabaseOperations(DatabaseOperations):
def lookup_cast(self, lookup_type):
if lookup_type in('icontains', 'istartswith'):
return "UPPER(unaccent(%s::text))"
else:
return super(DatabaseOperations, self).lookup_cast(lookup_type)
class DatabaseWrapper(DatabaseWrapper):
def __init__(self, *args, **kwargs):
super(DatabaseWrapper, self).__init__(*args, **kwargs)
self.operators['icontains'] = 'LIKE UPPER(unaccent(%s))'
self.operators['istartswith'] = 'LIKE UPPER(unaccent(%s))'
self.ops = DatabaseOperations(self)
Use this file base.py
in a folder and use this folder as db backend. icontains and istartswith are now case and accent insensitive.
这篇关于如何在带有postgres的Django中使用不区分重音的过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!