Filter a field
notes : Not all fuzzy filters are exact matches .
pip install django-filter==21.1
notes : The version is 21.1, Other versions cannot be used
2. To configure
stay settings.py Medium INSTSLL_APPS add to
'django_filters',
from django_filters import FilterSet
from .models import Library name
class Library name Filter(FilterSet):
class Meta:
model = Faculty
fields = (' Field ',)
# notes : Fields to filter
from django_filters.rest_framework import DjangoFilterBackend
from .filter import Library name Filter
class Library name ViewSet(ModelViewSet):
queryset = Library name .objects.all()
serializer_class = Library name Serializer
filter_backends = (DjangoFilterBackend,)
# Set up... For personalized views , You can also set it globally
filter_class = Library name Filter
Global settings filter_backends
# stay settings.py Add
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS':[
'django_filters.rest_framework.DjangoFilterBackend',
],
}
notes : Rewrite the filter code in the filter class
from django_filters import FilterSet,filters
from .models import Library name
#filters Is a fuzzy query class
class FacultyFilter(FilterSet):
Field name = filters.CharFilter(field_name=' Field name ',lookup_expr='icontains')
class Meta:
model = Library name
fields = (' Field name ',)
p{margin:10px 0}.markdown-body
Recently, I received a network