I need to filter Titles by genre field, I tried to use DjangoFilterBackend, but it didn’t work, I don’t know, how to create a CustomSearchFilter?

  django, django-rest-framework, python

I need to filter Titles by genre field, I tried to use DjangoFilterBackend, but it didn’t work, I don’t know, how to create a CustomSearchFilter?

Views:

class GenreViewSet(ModelCVDViewSet):
    queryset = Genre.objects.all()
    serializer_class = GenreSerializer
    permission_classes = (IsAdminOrReadOnly,)
    filter_backends = (DjangoFilterBackend, filters.SearchFilter)
    filterset_fields = ('name', 'slug')
    search_fields = ('name', 'slug')
    lookup_field = 'slug'


class TitleViewSet(viewsets.ModelViewSet):
    queryset = Title.objects.all()
    serializer_class = TitleSerializer
    permission_classes = (IsAdminOrReadOnly,)
    filter_backends = (DjangoFilterBackend, filters.SearchFilter)
    filterset_fields = ('genre__slug', 'category__slug', 'name', 'year')
    search_fields = ('=genre__slug', '=category__slug', 'name', 'year')

Source: Python Questions

LEAVE A COMMENT