Django结合__unaccent和__search查找 [英] Django Combining __unaccent and __search Lookups
问题描述
因此,我尝试在同一模型过滤器中使用 __ unaccent
和 __ search
,但是在以下情况下收到错误消息这样做。我正在尝试使用术语神奇宝贝匹配术语神奇宝贝(注意é)
So I am trying to use __unaccent
and __search
in the same model filter, but I receive an error when doing so. I am trying to make a filter using the term "Pokemon" match the term "Pokémon" (notice the "é")
Game.objects .filter(title__unaccent__icontains = Pokemon)
可以正常工作,但是当我使用 Game.objects.filter(title__unaccent__search = Pokemon)
时,我得到了以下错误:
Game.objects.filter(title__unaccent__icontains="Pokemon")
works fine, but when I use Game.objects.filter(title__unaccent__search="Pokemon)
, I get the following error:
在/ autocomplete-games /函数unaccent(tsquery)中出现ProgrammingError
ProgrammingError at /autocomplete-games/ function unaccent(tsquery)
不存在第1行:... ALESCE(UNACCENT( main_game。 title),''))
@@(UNACCENT(p ...
^提示:否函数匹配给定的名称和参数类型。您可能需要
来添加显式类型强制转换。
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
Python 3.5
Django 1.10
Django 1.10
PostgreSQL(
PostgreSQL (unaccent extension installed)
推荐答案
做到这一点的方法是在数据库中定义自定义搜索配置,例如:
The way to do this is to define custom search configuration in database like:
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/
I am not a postgres expert, but this configuration works for me. For more details check tutorial like this: http://www.nomadblue.com/blog/django/from-like-to-full-text-search-part-ii/
而不是在Django中使用它:
and than use this in 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'))
这篇关于Django结合__unaccent和__search查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!