我有一个
Pattern
对象的数据库,我想获取句子中包含字段 Pattern.substring
的所有模式:“这句话必须包含所有返回的 Pattern 对象”。
因此它返回例如具有这些
Pattern
值的 .substring
对象:
“这句话”,“全部”,“ce全部返回”....
所以我需要像反向查找这样的东西。据我所知,
__search
应该这样做,但它会引发错误:
Pattern.objects.filter(substring__search="This sentence has to contain all returned Pattern objects")
虽然我使用
django.db.backends.postgresql
,但它引发了:
FieldError:不支持对 CharField 进行查找“搜索”或不允许在该字段上进行连接。
你知道如何让它发挥作用吗?
完整追溯:
FieldError Traceback (most recent call last)
<ipython-input-16-80407f61e9e0> in <module>()
----> 1 Pattern.objects.filter(substring__search="This sentence has to contain all returned Pattern objects")
~/.virtualenvs/ticketscrawler/lib/python3.6/site-packages/django/db/models/manager.py in manager_method(self, *args, **kwargs)
80 def create_method(name, method):
81 def manager_method(self, *args, **kwargs):
---> 82 return getattr(self.get_queryset(), name)(*args, **kwargs)
83 manager_method.__name__ = method.__name__
84 manager_method.__doc__ = method.__doc__
~/.virtualenvs/ticketscrawler/lib/python3.6/site-packages/django/db/models/query.py in filter(self, *args, **kwargs)
834 set.
835 """
--> 836 return self._filter_or_exclude(False, *args, **kwargs)
837
838 def exclude(self, *args, **kwargs):
~/.virtualenvs/ticketscrawler/lib/python3.6/site-packages/django/db/models/query.py in _filter_or_exclude(self, negate, *args, **kwargs)
852 clone.query.add_q(~Q(*args, **kwargs))
853 else:
--> 854 clone.query.add_q(Q(*args, **kwargs))
855 return clone
856
~/.virtualenvs/ticketscrawler/lib/python3.6/site-packages/django/db/models/sql/query.py in add_q(self, q_object)
1251 # So, demotion is OK.
1252 existing_inner = {a for a in self.alias_map if self.alias_map[a].join_type == INNER}
-> 1253 clause, _ = self._add_q(q_object, self.used_aliases)
1254 if clause:
1255 self.where.add(clause, AND)
~/.virtualenvs/ticketscrawler/lib/python3.6/site-packages/django/db/models/sql/query.py in _add_q(self, q_object, used_aliases, branch_negated, current_negated, allow_joins, split_subq)
1275 child, can_reuse=used_aliases, branch_negated=branch_negated,
1276 current_negated=current_negated, allow_joins=allow_joins,
-> 1277 split_subq=split_subq,
1278 )
1279 joinpromoter.add_votes(needed_inner)
~/.virtualenvs/ticketscrawler/lib/python3.6/site-packages/django/db/models/sql/query.py in build_filter(self, filter_expr, branch_negated, current_negated, can_reuse, allow_joins, split_subq, reuse_with_filtered_relation)
1213 col = targets[0].get_col(alias, join_info.final_field)
1214
-> 1215 condition = self.build_lookup(lookups, col, value)
1216 lookup_type = condition.lookup_name
1217 clause.add(condition, AND)
~/.virtualenvs/ticketscrawler/lib/python3.6/site-packages/django/db/models/sql/query.py in build_lookup(self, lookups, lhs, rhs)
1077 # A lookup wasn't found. Try to interpret the name as a transform
1078 # and do an Exact lookup against it.
-> 1079 lhs = self.try_transform(lhs, lookup_name)
1080 lookup_name = 'exact'
1081 lookup_class = lhs.get_lookup(lookup_name)
~/.virtualenvs/ticketscrawler/lib/python3.6/site-packages/django/db/models/sql/query.py in try_transform(self, lhs, name)
1113 "Unsupported lookup '%s' for %s or join on the field not "
1114 "permitted." %
-> 1115 (name, lhs.output_field.__class__.__name__))
1116
1117 def build_filter(self, filter_expr, branch_negated=False, current_negated=False,
FieldError: Unsupported lookup 'search' for CharField or join on the field not permitted.
从 django 文档来看,此查询查找仅支持 MYSQL,并且从 django 1.10 开始已弃用。 https://docs.djangoproject.com/en/1.10/releases/1.10/#search-query-lookup。该链接上也提出了解决方案
from django.db import models
class Search(models.Lookup):
lookup_name = 'search'
def as_mysql(self, compiler, connection):
lhs, lhs_params = self.process_lhs(compiler, connection)
rhs, rhs_params = self.process_rhs(compiler, connection)
params = lhs_params + rhs_params
return 'MATCH (%s) AGAINST (%s IN BOOLEAN MODE)' % (lhs, rhs), params
models.CharField.register_lookup(Search)
models.TextField.register_lookup(Search)