Python – Django/DRF application receives AttributeError : ‘function’ object has no attribute ‘get_extra_actions’

Django/DRF application receives AttributeError : ‘function’ object has no attribute ‘get_extra_actions’… here is a solution to the problem.

Django/DRF application receives AttributeError : ‘function’ object has no attribute ‘get_extra_actions’

I’m new to web app development and am trying Django. I implemented an API using the Django Rest Framework and it seemed to work fine at first.

I

tried adding some bootstraps to connect to my frontend (React) and now when I run Django with “python manage.py runserver” I get an AttributeError. I’m not sure how to debug this bug.

Below is the full stack trace, with my views.py and urls.py for context.

Stack trace

Unhandled exception in thread started by <function check_errors.<locals>.wrapper
 at 0x000000560AAAC488>
Traceback (most recent call last):
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\django\utils\autore
load.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\django\core\managem
ent\commands\runserver.py", line 120, in inner_run
    self.check(display_num_errors=True)
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\django\core\managem
ent\base.py", line 364, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\django\core\managem
ent\base.py", line 351, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\django\core\checks\
registry.py", line 73, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\django\core\checks\
urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\django\core\checks\
urls.py", line 23, in check_resolver
    return check_method()
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\django\urls\resolve
rs.py", line 397, in check
    for pattern in self.url_patterns:
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\django\utils\functi
onal.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\django\urls\resolve
rs.py", line 536, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\django\utils\functi
onal.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\django\urls\resolve
rs.py", line 529, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Users\aidancain\envs\usaproject\lib\importlib\__init__.py", line 126,
 in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\ATC\envs\usaproject\Scripts\BaldEagle\usabaseball\urls.py", lin
e 24, in <module>
    path('api/', include('api.urls')),
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\django\urls\conf.py
", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "C:\Users\aidancain\envs\usaproject\lib\importlib\__init__.py", line 126,
 in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\ATC\envs\usaproject\Scripts\BaldEagle\api\urls.py", line 16, in
 <module>
    urlpatterns += router.urls
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\rest_framework\rout
ers.py", line 101, in urls
    self._urls = self.get_urls()
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\rest_framework\rout
ers.py", line 363, in get_urls
    urls = super(DefaultRouter, self).get_urls()
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\rest_framework\rout
ers.py", line 261, in get_urls
    routes = self.get_routes(viewset)
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\rest_framework\rout
ers.py", line 176, in get_routes
    extra_actions = viewset.get_extra_actions()
AttributeError: 'function' object has no attribute 'get_extra_actions'

api.views.py

from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
from rest_framework.response import Response
from rest_framework import status
from rest_framework import generics, mixins
from rest_framework import viewsets
from api.models import Playerbios, Testtrackman
from api.serializers import USASerializer, TrackmanSerializer

# @method_decorator(login_required, name='dispatch')
@csrf_exempt
class USAViewset(viewsets. ModelViewSet):
    queryset = Playerbios.objects.all()
    serializer_class = USASerializer
    lookup_field = 'trackmanid'

# @method_decorator(login_required, name='dispatch')
@csrf_exempt
class TrackmanViewset(viewsets. ModelViewSet):
    queryset = Testtrackman.objects.all()
    serializer_class = TrackmanSerializer
    lookup_field = 'pitchuid'

api.urls.py

from django.conf.urls import url, include
from rest_framework import routers
#from .views import USAView, USAListView
from .views import USAViewset, TrackmanViewset

# urlpatterns = [
#     url('(? P<trackmanid>)/', USAView.as_view(), name='usa-rud-view'),
#     url('', USAListView.as_view(), name='usa-list-view'),
# ]
urlpatterns = [

]
router = routers. DefaultRouter()
router.register(r'bios', USAViewset)
router.register(r'trackman', TrackmanViewset)
urlpatterns += router.urls

Solution

You cannot use csrf_exempt as a class decorator.

DRF View is not CSRF protected anyway. You should only remove the decorators.

Related Problems and Solutions