I have a database of Pattern
objects and I want to get all patterns which has field Pattern.substring
contained by sentence: "This sentence has to contain all returned Pattern objects".
So it returns for example Pattern
objects with these .substring
values:
"This sentence","all", "ce all returned" ....
So I need something like reverse lookup. As far as I know, __search
should do that but it raises error:
Pattern.objects.filter(substring__search="This sentence has to contain all returned Pattern objects")
Although I use django.db.backends.postgresql
, it raises:
FieldError: Unsupported lookup 'search' for CharField or join on the field not permitted.
Do you know how to make it work?
Full traceback:
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.
From the django documentation, this query lookup only supports MYSQL and that is deprecated as of django 1.10. https://docs.djangoproject.com/en/1.10/releases/1.10/#search-query-lookup. The solution is proposed on that link as well