Reputation: 4192
I am wanting to add some search functionality to my API and I followed this simple guide but I'm still getting the error
type object 'ClientViewSet' has no attribute 'get_extra_actions'
Versions
models.py
class Client(models.Model):
phone = models.CharField(max_length=10)
urls.py
router = routers.DefaultRouter()
router.register(r'clients', ClientViewSet)
urlpatterns = [
path('api/', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
]
serializers.py
class ClientSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Client
fields = '__all__'
views.py
class ClientViewSet(generics.ListAPIView):
queryset = Client.objects.all()
serializer_class = ClientSerializer
filter_backends = [filters.SearchFilter]
search_fields = ['phone']
Upvotes: 1
Views: 3057
Reputation: 4192
To build off the answer provided here, a ViewSet
needs to inherit from a ViewSet
and the ListAPIView
class does not inherit from ViewSet
.
A way to get this to work, however, is to change the inherited class from ListAPIView
to ModelViewSet
like this:
class ClientViewSet(viewsets.ModelViewSet):
queryset = Client.objects.all()
serializer_class = ClientSerializer
filter_backends = [DjangoFilterBackend]
filter_fields = ['phone']
Upvotes: 1
Reputation: 11
"You've called it a viewset, but that doesn't make it one; you inherit from APIView which is a standalone generic view, not a viewset.
A viewset needs to inherit from viewsets.ViewSet."
https://stackoverflow.com/a/49721133/8932675
Upvotes: 1