python - Django 结合 __unaccent 和 __search 查询

标签 python django postgresql orm

因此,我尝试在同一个模型过滤器中使用 __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/

相关文章:

python - 如何通过 xmlrpc 安全地获得对 supervisord 的编程访问?

django change_form.html

sql - Max() 的自定义顺序

sql - 在 postgres 中加入性能

.net - Entity Framework ObjectContext -> 对 native DBMS 的原始 SQL 调用

php - Python 作为网络脚本语言?

python - 在 Python panda 中从两列创建数据框时出错

php - python和php可以访问同一个mysqldb吗?

django - 如何在 Django 中支持 AutoField(primary_key=False)?

python - Django REST 框架元组被解释为字符串?