Asaduzzaman Sohel
Asaduzzaman Sohel

Reputation: 588

django rest issue: get_extra_actions with listApiView

I'm working on a project using the Django REST API framework. When I run the project locally I get the following error:

File "/home/asad/PycharmProjects/microshop/venv/lib/python3.8/site-packages/rest_framework/routers.py", line 153, in get_routes
    extra_actions = viewset.get_extra_actions()
AttributeError: type object 'CustomerListAPIViewSet' has no attribute 'get_extra_actions'

I searched on Google and StackOverflow, but could not found any solution.

serializers.py

class CustomerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Customer
        fields = ['name', 'phone_number', 'address', ]

views.py

class CustomerListAPIViewSet(generics.ListAPIView):
    queryset = Customer.objects.all()
    serializer_class = CustomerSerializer

urls.py

from django.urls import path, include
from accounts_app.views import CustomerListAPIViewSet
from rest_framework import routers

router = routers.DefaultRouter()

router.register(r'customer/', CustomerListAPIViewSet)

urlpatterns = router.urls

Upvotes: 2

Views: 1400

Answers (1)

scriptmonster
scriptmonster

Reputation: 2771

Your problem is with your view's naming. This create a confusion.

You are actually creating an APIView subclass. But you are naming it as ViewSet. You can register ViewSet classes with rest_framework routers not APIViews.

You can do following changes to make your view runnable.

Views File:

class CustomerListAPIView(generics.ListAPIView):
    queryset = Customer.objects.all()
    serializer_class = CustomerSerializer

URLS File:

from django.urls import path, include
from accounts_app.views import CustomerListAPIView

urlpatterns = [
    path(r'^customer/', CustomerListAPIView.as_view(), name='customer-list')
]

The other way around is defining an actual ViewSet class, but the structure will be different.

Upvotes: 4

Related Questions