【问题标题】:Full Text search on Django Rest Framework only supported for MYSQL?Django Rest Framework 上的全文搜索仅支持 MYSQL?
【发布时间】:2014-12-09 00:32:39
【问题描述】:
【问题讨论】:
标签:
django
api
django-rest-framework
【解决方案1】:
自从@stalk 回答后,情况发生了变化。 As of Django 1.10,Django Rest Framework 也可以支持 PostgreSQL。
由于django.contrib.postgres 提供与 MySQL 相同的__search interface,您可以使用与 MySQL 相同的设置将 API 连接到 Postgres:
事实上,根据@stalk 原始答案中 Django 文档链接上的弃用说明,听起来 Django 对 MySQL 的全文搜索支持要么已弃用,要么至少正在改变。该注释链接到以下说明,其中还包含替换它的示例代码:
不推荐使用仅支持 MySQL 且功能极其有限的搜索查找。将其替换为自定义查找...
抱歉,我没有足够的声誉来发布其他直接链接。
【解决方案2】:
那是因为 django-rest-framework 使用了 django 的__search。
来自django-rest-framework的master中的当前最新提交:
def construct_search(self, field_name):
if field_name.startswith('^'):
return "%s__istartswith" % field_name[1:]
elif field_name.startswith('='):
return "%s__iexact" % field_name[1:]
elif field_name.startswith('@'):
return "%s__search" % field_name[1:]
else:
return "%s__icontains" % field_name
而django docs 讲述了__search(布尔全文搜索):
Note this is only available in MySQL and requires direct manipulation of the database
to add the full-text index. By default Django uses BOOLEAN MODE for full text searches.
See the MySQL documentation for additional details.