django-filter finds a match if you enter the full name. For example, I try to filter by the name "Titanic" and I type "Titan" into the search, nothing will be found until I type in the entire text "Titanic". How do I search by partial match?
class ProjectFilter(django_filters.FilterSet):
address__name = django_filters.CharFilter(field_name='address', lookup_expr='street')
approve = django_filters.BooleanFilter(field_name='approve')
ordering = django_filters.OrderingFilter(choices=CHOICES, required=True, empty_label=None,)
class Meta:
model = Project
exclude = [field.name for field in Project._meta.fields]
order_by_field = 'address'
View
class FilterTable(SingleTableMixin, FilterView):
table_class = TableAll
model = Project
template_name = "table.html"
filterset_class = ProjectFilter
CodePudding user response:
By default it uses exact
lookup. https://django-filter.readthedocs.io/en/stable/ref/filters.html#lookup-expr Try to use icontains
for example https://docs.djangoproject.com/en/4.1/ref/models/querysets/#icontains
CodePudding user response:
By default django filter data with exact match if you want to filter with partial query match you need to add icontains lookup expression
icontains provided filter data from queryset with partial string match.
So, your code becomes like this...
You need to add lookup_expr='icontains'
instead of lookup_expr='street'
class ProjectFilter(django_filters.FilterSet):
address__name = django_filters.CharFilter(field_name='address', lookup_expr='icontains')