Search code examples
reactjsdjangodjango-rest-frameworkdjango-viewsdjango-viewsets

django rest framework AttributeError: 'function' object has no attribute 'get_extra_actions' when accessing from react


I seem to be getting the following error, but seemingly only when I access from React.

2024-04-19 18:30:28   File "/app/config/urls.py", line 29, in <module>
2024-04-19 18:30:28     path('api/', include(router.urls)),
2024-04-19 18:30:28                            ^^^^^^^^^^^
2024-04-19 18:30:28   File "/usr/local/lib/python3.12/site-packages/rest_framework/routers.py", line 77, in urls
2024-04-19 18:30:28     self._urls = self.get_urls()
2024-04-19 18:30:28                  ^^^^^^^^^^^^^^^
2024-04-19 18:30:28   File "/usr/local/lib/python3.12/site-packages/rest_framework/routers.py", line 338, in get_urls
2024-04-19 18:30:28     urls = super().get_urls()
2024-04-19 18:30:28            ^^^^^^^^^^^^^^^^^^
2024-04-19 18:30:28   File "/usr/local/lib/python3.12/site-packages/rest_framework/routers.py", line 236, in get_urls
2024-04-19 18:30:28     routes = self.get_routes(viewset)
2024-04-19 18:30:28              ^^^^^^^^^^^^^^^^^^^^^^^^
2024-04-19 18:30:28   File "/usr/local/lib/python3.12/site-packages/rest_framework/routers.py", line 152, in get_routes
2024-04-19 18:30:28     extra_actions = viewset.get_extra_actions()
2024-04-19 18:30:28                     ^^^^^^^^^^^^^^^^^^^^^^^^^
2024-04-19 18:30:28 AttributeError: 'function' object has no attribute 'get_extra_actions'

urls.py:

router.register(r'hello', views.HelloViewSet, 'hello')

views.py:

@api_view(['GET'])
@permission_classes([AllowAny])
class HelloViewSet(viewsets.ModelViewSet):

    #dummy serializers and queryset for now
    serializer_class = SomeDummyDetailSerializer
    queryset = SomeDummyObject.objects.all()

    permission_classes = [DjangoObjectPermissions]
    def list(self, request): #get list
        return Response({'message': 'Hello, worlds!'})
    
    def retrieve(self, request, pk=None): #get detail
        return Response({'message': 'Hello, worlds!'})

Am I missing something here? Most errors I see with this seem to be using views instead of ViewSets with router. I also seemingly need to set the @api_view to override 401 denied errors, but I assume that's a separate issue with token authentication or soemthing.


Solution

  • Solved it - had to remove @api_view and do it inside the class

    permission_classes = [AllowAny]
    http_method_names = ['get']