0
votes

I have created a function based view since it does not require model queryset directly.

graph_data/views.py

@api_view(http_method_names=['GET'])
def my_func(request):

    print(request)

    return Response({'message': 'success'})

And in the graph_data/urls.py

from django.urls import path

from graph_data.views import my_func

app_name = 'graph_data'
urlpatterns = [
    path('my_func/<contact_id>', my_func, name='my_func')
]

and included graph url in main app urls.py

router = routers.SimpleRouter()
router.register(r'graph/', include('graph_data.urls'), base_name='graph_data')

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

But this gives

AttributeError: 'tuple' object has no attribute 'get_extra_actions'

Full traceback

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x10b222378>
Traceback (most recent call last):
  File "/path_to/share/virtualenvs/my-app-gPhPKdWh/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "/path_to/share/virtualenvs/my-app-gPhPKdWh/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
    self.check(display_num_errors=True)
  File "/path_to/share/virtualenvs/my-app-gPhPKdWh/lib/python3.6/site-packages/django/core/management/base.py", line 379, in check
    include_deployment_checks=include_deployment_checks,
  File "/path_to/share/virtualenvs/my-app-gPhPKdWh/lib/python3.6/site-packages/django/core/management/base.py", line 366, in _run_checks
    return checks.run_checks(**kwargs)
  File "/path_to/share/virtualenvs/my-app-gPhPKdWh/lib/python3.6/site-packages/django/core/checks/registry.py", line 71, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/path_to/share/virtualenvs/my-app-gPhPKdWh/lib/python3.6/site-packages/django/core/checks/urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "/path_to/share/virtualenvs/my-app-gPhPKdWh/lib/python3.6/site-packages/django/core/checks/urls.py", line 23, in check_resolver
    return check_method()
  File "/path_to/share/virtualenvs/my-app-gPhPKdWh/lib/python3.6/site-packages/django/urls/resolvers.py", line 396, in check
    for pattern in self.url_patterns:
  File "/path_to/share/virtualenvs/my-app-gPhPKdWh/lib/python3.6/site-packages/django/utils/functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/path_to/share/virtualenvs/my-app-gPhPKdWh/lib/python3.6/site-packages/django/urls/resolvers.py", line 533, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/path_to/share/virtualenvs/my-app-gPhPKdWh/lib/python3.6/site-packages/django/utils/functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/path_to/share/virtualenvs/my-app-gPhPKdWh/lib/python3.6/site-packages/django/urls/resolvers.py", line 526, in urlconf_module
    return import_module(self.urlconf_name)
  File "/path_to/share/virtualenvs/my-app-gPhPKdWh/lib/python3.6/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 "/path_to/my-app/src/my_app/urls.py", line 55, in <module>
    path('', include(router.urls)),
  File "/Upath_to/share/virtualenvs/my-app-gPhPKdWh/lib/python3.6/site-packages/rest_framework/routers.py", line 101, in urls
    self._urls = self.get_urls()
  File "/path_to/share/virtualenvs/my-app-gPhPKdWh/lib/python3.6/site-packages/rest_framework/routers.py", line 261, in get_urls
    routes = self.get_routes(viewset)
  File "/path_to/share/virtualenvs/my-app-gPhPKdWh/lib/python3.6/site-packages/rest_framework/routers.py", line 176, in get_routes
    extra_actions = viewset.get_extra_actions()
AttributeError: 'tuple' object has no attribute 'get_extra_actions'
1
Please show the full traceback. - Daniel Roseman
My first guess is that you have a tailing comma somewhere wrapping a result in a tuple - Willem Van Onsem
Added full traceback - Anuj TBE

1 Answers

1
votes

Router's register function takes a viewset, so not possible to include graph's url the way you'd like.

get_extra_actions is method of ViewSet class and include returns a tuple. So you this error because it tries to call this function on the tuple returned from include when building the routes.

Include your graph urls like this instead:

urlpatterns = [
    path(r'graph/', include(('graph_data.urls','graph_data'), namespace='graph_data')),
]