因此,我尝试在同一个模型过滤器中使用 __unaccent
和 __search
,但这样做时我收到错误消息。我正在尝试使用术语“Pokemon”匹配术语“Pokémon”(注意“é”)
Game.objects.filter(title__unaccent__icontains="Pokemon")
工作正常,但是当我使用 Game.objects.filter(title__unaccent__search="Pokemon)
时,我得到以下错误:
ProgrammingError at /autocomplete-games/ function unaccent(tsquery)
does not exist LINE 1: ...ALESCE(UNACCENT("main_game"."title"), '')) @@ (UNACCENT(p... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts.
我正在使用:
python 3.5
Django 1.10
PostgreSQL(安装了 unaccent 扩展)
最佳答案
这样做的方法是在数据库中定义自定义搜索配置,例如:
CREATE TEXT SEARCH CONFIGURATION unaccent ( COPY = french );
ALTER TEXT SEARCH CONFIGURATION unaccent ALTER MAPPING FOR hword, hword_part, word WITH unaccent, simple;
我不是 postgres 专家,但这个配置对我有用。有关详细信息,请查看如下教程:http://www.nomadblue.com/blog/django/from-like-to-full-text-search-part-ii/
然后在 Django 中使用它:
from django.contrib.postgres.search import SearchVector, SearchQuery
Game.objects.annotate(unaccent_title=SearchVector('title', config='unaccent')).filter(unaccent_title=SearchQuery('Pokemon', config='unaccent'))
关于python - Django 结合 __unaccent 和 __search 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42421706/