Django-filter, how to make multiple fields search? (with django-filter!)
Question:
How can I make multiple fields search with Django-filter from model like:
class Location(models.Model):
loc = models.CharField(max_length=100, blank=True)
loc_mansioned = models.CharField(max_length=100, blank=True)
loc_country = models.CharField(max_length=100, blank=True)
loc_modern = models.CharField(max_length=100, blank=True)
I need one input field on my website, that can search over all fields of Location model
Answers:
You can probably create a custom filter and do something like this:
from django.db.models import Q
import django_filters
class LocationFilter(django_filters.FilterSet):
q = django_filters.CharFilter(method='my_custom_filter', label="Search")
class Meta:
model = Location
fields = ['q']
def my_custom_filter(self, queryset, name, value):
return queryset.filter(
Q(loc__icontains=value) | Q(loc_mansioned__icontains=value) | Q(loc_country__icontains=value) | Q(loc_modern__icontains=value)
)
This would filter by any of of those fields. You can replace the icontains
with whatever you want.
Due that you’ve defined Location as an object, to filter by multiple fields just use the filter
method.
filterlocation = Location.objects.filter(loc=formloc, loc_mansioned=formlocmansioned, loc_country=formloccountry, loc_modern=formlocmodern)
But you need to implement a better way to use this filters, so only the result that have all conditions will be displayed.
This is perfect. I’m trying to do a dynamic filter, with a switch to get one more field in the search if checked. Something like this:
def my_custom_filter(self, queryset, name, value):
return Reference.objects.filter(
Q(ref_title__icontains=value))
def my_custom_filter_with_description(self, queryset, name, value):
return Reference.objects.filter(
Q(ref_title__icontains=value) | Q(complete_description__icontains=value))
But I have no clue how to link the switch to the class
Another solution, since the other one was not working directly:
@staticmethod
def filter_stock(qs, name, value):
return qs.filter(
Q(ticker__exact=value) | Q(company__iexact=value)
)
How can I make multiple fields search with Django-filter from model like:
class Location(models.Model):
loc = models.CharField(max_length=100, blank=True)
loc_mansioned = models.CharField(max_length=100, blank=True)
loc_country = models.CharField(max_length=100, blank=True)
loc_modern = models.CharField(max_length=100, blank=True)
I need one input field on my website, that can search over all fields of Location model
You can probably create a custom filter and do something like this:
from django.db.models import Q
import django_filters
class LocationFilter(django_filters.FilterSet):
q = django_filters.CharFilter(method='my_custom_filter', label="Search")
class Meta:
model = Location
fields = ['q']
def my_custom_filter(self, queryset, name, value):
return queryset.filter(
Q(loc__icontains=value) | Q(loc_mansioned__icontains=value) | Q(loc_country__icontains=value) | Q(loc_modern__icontains=value)
)
This would filter by any of of those fields. You can replace the icontains
with whatever you want.
Due that you’ve defined Location as an object, to filter by multiple fields just use the filter
method.
filterlocation = Location.objects.filter(loc=formloc, loc_mansioned=formlocmansioned, loc_country=formloccountry, loc_modern=formlocmodern)
But you need to implement a better way to use this filters, so only the result that have all conditions will be displayed.
This is perfect. I’m trying to do a dynamic filter, with a switch to get one more field in the search if checked. Something like this:
def my_custom_filter(self, queryset, name, value):
return Reference.objects.filter(
Q(ref_title__icontains=value))
def my_custom_filter_with_description(self, queryset, name, value):
return Reference.objects.filter(
Q(ref_title__icontains=value) | Q(complete_description__icontains=value))
But I have no clue how to link the switch to the class
Another solution, since the other one was not working directly:
@staticmethod
def filter_stock(qs, name, value):
return qs.filter(
Q(ticker__exact=value) | Q(company__iexact=value)
)