首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django trigram_similar

Django trigram_similar
EN

Stack Overflow用户
提问于 2018-11-12 03:15:34
回答 1查看 363关注 0票数 0

我一直在尝试在我的Django后端代码中实现trigram_similar功能。我得到一个错误:

代码语言:javascript
复制
web_1         | Traceback (most recent call last):

web_1         |   File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner

web_1         |     response = get_response(request)

web_1         |   File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response

web_1         |     response = self.process_exception_by_middleware(e, request)

web_1         |   File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response

web_1         |     response = wrapped_callback(request, *callback_args, **callback_kwargs)

web_1         |   File "/usr/local/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view

web_1         |     return view_func(*args, **kwargs)

web_1         |   File "/usr/local/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view

web_1         |     return self.dispatch(request, *args, **kwargs)

web_1         |   File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 489, in dispatch

web_1         |     response = self.handle_exception(exc)

web_1         |   File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 449, in handle_exception

web_1         |     self.raise_uncaught_exception(exc)

web_1         |   File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 486, in dispatch

web_1         |     response = handler(request, *args, **kwargs)

web_1         |   File "/usr/local/lib/python3.6/site-packages/rest_framework/generics.py", line 201, in get

web_1         |     return self.list(request, *args, **kwargs)

web_1         |   File "/usr/local/lib/python3.6/site-packages/rest_framework/mixins.py", line 40, in list

web_1         |     queryset = self.filter_queryset(self.get_queryset())

web_1         |   File "/usr/local/lib/python3.6/site-packages/profilehooks.py", line 239, in new_fn

web_1         |     return fp(*args, **kw)

web_1         |   File "/usr/local/lib/python3.6/site-packages/profilehooks.py", line 347, in __call__

web_1         |     return profiler.runcall(self.fn, *args, **kw)

web_1         |   File "/usr/local/lib/python3.6/cProfile.py", line 109, in runcall

web_1         |     return func(*args, **kw)

web_1         |   File "/code/src/users/views.py", line 266, in filter_queryset

web_1         |     major__name__trigram_similar = "CS"

web_1         |   File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 784, in filter

web_1         |     return self._filter_or_exclude(False, *args, **kwargs)

web_1         |   File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 802, in _filter_or_exclude

web_1         |     clone.query.add_q(Q(*args, **kwargs))

web_1         |   File "/usr/local/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1250, in add_q

web_1         |     clause, _ = self._add_q(q_object, self.used_aliases)

web_1         |   File "/usr/local/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1276, in _add_q

web_1         |     allow_joins=allow_joins, split_subq=split_subq,

web_1         |   File "/usr/local/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1210, in build_filter

web_1         |     condition = self.build_lookup(lookups, col, value)

web_1         |   File "/usr/local/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1102, in build_lookup

web_1         |     lhs = self.try_transform(lhs, name, lookups)

web_1         |   File "/usr/local/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1120, in try_transform

web_1         |     (name, lhs.output_field.__class__.__name__))

web_1         | django.core.exceptions.FieldError: Unsupported lookup 'trigram_similar' for CharField or join on the field not permitted.

web_1         | 2018-11-11 19:03:58,144 ERROR Internal Server Error: /mentors/

web_1         | Traceback (most recent call last):

web_1         |   File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner

web_1         |     response = get_response(request)

web_1         |   File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response

web_1         |     response = self.process_exception_by_middleware(e, request)

web_1         |   File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response

web_1         |     response = wrapped_callback(request, *callback_args, **callback_kwargs)

web_1         |   File "/usr/local/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view

web_1         |     return view_func(*args, **kwargs)

web_1         |   File "/usr/local/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view

web_1         |     return self.dispatch(request, *args, **kwargs)

web_1         |   File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 489, in dispatch

web_1         |     response = self.handle_exception(exc)

web_1         |   File "/usr/local/lib/python3.6/site-
packages/rest_framework/views.py", line 449, in handle_exception

web_1         |     self.raise_uncaught_exception(exc)

web_1         |   File "/usr/local/lib/python3.6/site-
packages/rest_framework/views.py", line 486, in dispatch

web_1         |     response = handler(request, *args, **kwargs)

web_1         |   File "/usr/local/lib/python3.6/site-
packages/rest_framework/generics.py", line 201, in get

web_1         |     return self.list(request, *args, **kwargs)

web_1         |   File "/usr/local/lib/python3.6/site-
packages/rest_framework/mixins.py", line 40, in list

web_1         |     queryset = self.filter_queryset(self.get_queryset())

web_1         |   File "/usr/local/lib/python3.6/site-
packages/profilehooks.py", line 239, in new_fn

web_1         |     return fp(*args, **kw)

web_1         |   File "/usr/local/lib/python3.6/site-
packages/profilehooks.py", line 347, in __call__

web_1         |     return profiler.runcall(self.fn, *args, **kw)

web_1         |   File "/usr/local/lib/python3.6/cProfile.py", line 109, 
in runcall

web_1         |     return func(*args, **kw)

web_1         |   File "/code/src/users/views.py", line 266, in 
filter_queryset

web_1         |     major__name__trigram_similar = "CS"

web_1         |   File "/usr/local/lib/python3.6/site-
packages/django/db/models/query.py", line 784, in filter

web_1         |     return self._filter_or_exclude(False, *args, **kwargs)

web_1         |   File "/usr/local/lib/python3.6/site-
packages/django/db/models/query.py", line 802, in _filter_or_exclude

web_1         |     clone.query.add_q(Q(*args, **kwargs))

web_1         |   File "/usr/local/lib/python3.6/site-
packages/django/db/models/sql/query.py", line 1250, in add_q

web_1         |     clause, _ = self._add_q(q_object, self.used_aliases)

web_1         |   File "/usr/local/lib/python3.6/site-
packages/django/db/models/sql/query.py", line 1276, in _add_q

web_1         |     allow_joins=allow_joins, split_subq=split_subq,

web_1         |   File "/usr/local/lib/python3.6/site-
packages/django/db/models/sql/query.py", line 1210, in build_filter

web_1         |     condition = self.build_lookup(lookups, col, value)

web_1         |   File "/usr/local/lib/python3.6/site-
packages/django/db/models/sql/query.py", line 1102, in build_lookup

web_1         |     lhs = self.try_transform(lhs, name, lookups)

web_1         |   File "/usr/local/lib/python3.6/site-
packages/django/db/models/sql/query.py", line 1120, in try_transform

web_1         |     (name, lhs.output_field.__class__.__name__))

web_1         | django.core.exceptions.FieldError: Unsupported lookup 'trigram_similar' for CharField or join on the field not permitted.

I've followed the instructions on this page https://docs.djangoproject.com/en/2.0/ref/contrib/postgres/lookups/ but to no avail. I have properly installed the dependencies and added the extra files.

     for item in query:
                     item_alias = trans_dict.get(item.lower(),item)
                     major_alias = major_dict.get(item.lower(),item)
                     queryset = queryset.filter(
                         major__name__trigram_similar = major_alias
                     )
EN

回答 1

Stack Overflow用户

发布于 2021-02-26 23:16:38

trigram_similar查找允许您使用专用的PostgreSQL扩展执行三元组查找,测量共享的三元组(三个连续字符)的数量。一个三元组查找被给予一个表达式,并返回具有大于当前相似性阈值的相似性度量的结果。

要使用它,请在INSTALLED_APPS中添加'django.contrib.postgres‘,并在PostgreSQL上激活pg_trgm扩展。您可以使用TrigramExtension迁移操作安装扩展。

您需要为存在以下问题的app.model创建新的空白迁移:

代码语言:javascript
复制
from django.contrib.postgres.operations import TrigramExtension

class Migration(migrations.Migration):
    operations = [
        TrigramExtension(),
    ]

这一切都必须手动完成(请确保在其中保留依赖项)。

代码语言:javascript
复制
python manage.py makemigrations <insert_app_name_here> --empty 

**注意:值得注意的是,您的数据库后端需要PostgreSQL...

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53252263

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档