Django __search查找导致FieldError

时间:2018-11-18 23:44:08

标签: python django database postgresql django-2.0

我有一个Pattern对象的数据库,我想获取句子中包含字段Pattern.substring的所有模式:“此句子必须包含所有返回的Pattern对象”。

因此它返回例如Pattern个具有以下.substring值的对象:

“这句话”,“全部”,“全部退还” ....

所以我需要类似反向查找的功能。据我所知,__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.

0 个答案:

没有答案