'AttributeError: type object has no attribute 'get_extra_actions'

I have a small web app, and I'm trying to develop an API for it. I'm having an issue with a model I have called Platform inside of an app I have called UserPlatforms. The same error: AttributeError: type object 'UserPlatformList' has no attribute 'get_extra_actions'

models.py:

class Platform(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL,
                             related_name='platform_owned',
                             on_delete=models.CASCADE,
                             default=10)

    PLATFORM_CHOICES = [platform_x, platform_y]

    platform_reference = models.CharField(max_length=10, choices=PLATFORM_CHOICES, default='platform_x')
    platform_variables = models.JSONField()

api/views.py

from .serializers import PlatformSerializer
from rest_framework import generics, permissions
from userplatforms.models import Platform

class UserPlatformList(generics.ListCreateAPIViewAPIView):
    queryset = Platform.objects.all()
    permisson_classes = [permissions.IsAuthenticatedOrReadOnly]
    serializer_class = PlatformSerializer

api/serializer.py

from rest_framework import serializers
from userplatforms.models import Platform
    
class PlatformSerializer(serializers.HyperlinkedModelSerializer):
        class Meta:
            model = Platform
            fields = '__all__'

api/urls.py

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

router = routers.DefaultRouter()
router.register(r'userplatforms/', views.UserPlatformList, 'userplatforms')
router.register(r'userfiles/', views.UserFileList, "userfiles")
router.register(r'users/', views.UserList, "user")



urlpatterns = [
    path("^", include(router.urls))
]


Solution 1:[1]

You cannot add generic Views in routers

So remove this line

router.register(r'userplatforms/', views.UserPlatformList, 'userplatforms')

and update urlpatterns to

urlpatterns = [
    path('userplatforms/', views.UserPlatformList, name='userplatforms'),
    path("^", include(router.urls))
]

Solution 2:[2]

change class PlatformSerializer(serializers.HyperlinkedModelSerializer): to class PlatformSerializer(serializers.ModelSerializer):

Solution 3:[3]

api/views.py

from .serializers import PlatformSerializer
from rest_framework import generics, permissions
from userplatforms.models import Platform

class UserPlatformList(generics.ListCreateAPIViewAPIView):
    queryset = Platform.objects.all()
    permisson_classes = [permissions.IsAuthenticatedOrReadOnly]
    serializer_class = PlatformSerializer

for the UserPlatformList inheritance, ListCreateAPIViewAPIView should be ListCreateAPIView

If you are using mixins to create the viewset, then it should be like

from .serializers import PlatformSerializer
from rest_framework import generics, permissions, viewsets
from userplatforms.models import Platform

class UserPlatformList(generics.ListModelMixin, viewsets.GenericViewSet):
    queryset = Platform.objects.all()
    permisson_classes = [permissions.IsAuthenticatedOrReadOnly]
    serializer_class = PlatformSerializer

This way router can be used to set the urlpatterns

api/urls.py

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

router = routers.DefaultRouter()
router.register(r'userplatforms/', views.UserPlatformList, 'userplatforms')
router.register(r'userfiles/', views.UserFileList, "userfiles")
router.register(r'users/', views.UserList, "user")


urlpatterns = router.urls

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sumithran
Solution 2 Krystian Kazimierczak
Solution 3 Gajaraj Sekar