自定义Django过滤器

自定义过滤器

pip install django-filter

filter.py

class TaskFilter(FilterSet):
    name = filters.CharFilter(lookup_expr='icontains')

    class Meta:
        model = Task
        fields = ['name', ]

settings.py

REST_FRAMEWORK = {
    'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend']
}

view.py

import django_filters.rest_framework
from django.contrib.auth.models import User
from myapp.serializers import UserSerializer
from rest_framework import generics

class TaskViewSet(BasicViewSet):
    queryset = Task.all()
    serializer_class = TaskSerializer
    filter_class = filter.TaskFilter
上一篇:标准化和归一化(综合)


下一篇:最近处理的一些函数