Django Rest Framework (GET filter on ManyToMany field)

后端 未结 1 823
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-14 19:12

I am trying to figure out how to filter a ManyToMany field by value. In Django, it is as simple as queryset.filter(m2mfield__name), but I can\'t quite figure out what I am missi

1条回答
  •  逝去的感伤
    2021-02-14 19:39

    Well, I found out what I was doing wrong. It was in the view. I had not declared filter_class on the view:

    from rest_framework.generics import ListAPIView, RetrieveAPIView
    from .serializers import StateSerializer
    from .filters import StateFilter
    
    class StateList(ListAPIView):
        queryset = State.objects.all()
        serializer_class = StateSerializer
        filter_fields = ('name', 'cities')
        filter_class = StateFilter  # This was missing
    

    I accidentally placed it on the serializer instead.

    0 讨论(0)
提交回复
热议问题