【问题标题】:Full Text search on Django Rest Framework only supported for MYSQL?Django Rest Framework 上的全文搜索仅支持 MYSQL?
【发布时间】:2014-12-09 00:32:39
【问题描述】:

当有像 Postgres 这样功能更强大的数据库时,为什么 Django Rest Framework 只支持对 MYSQL 后端的全文搜索,我读了这篇文章并感到惊讶:/

http://www.django-rest-framework.org/api-guide/filtering

"@'全文搜索。(目前只支持Django的MySQL后端。)"

知道这背后的任何具体原因吗?

【问题讨论】:

    标签: 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:

    • django.contrib.postgres 添加到您的INSTALLED_APPS
    • 在您看来,使用@ 符号来标记全文搜索字段:

      search_fields = ('@title', '@description')
      

    事实上,根据@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.
      

      【讨论】:

        猜你喜欢
        • 2011-01-15
        • 1970-01-01
        • 2017-09-07
        • 1970-01-01
        • 2020-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多