代码之家  ›  专栏  ›  技术社区  ›  Anuj TBE

AttributeError:“tuple”对象在Django Rest框架中没有基于函数视图的“get\u extra\u actions”属性

  •  0
  • Anuj TBE  · 技术社区  · 7 年前

    图形\u数据/视图.py

    @api_view(http_method_names=['GET'])
    def my_func(request):
    
        print(request)
    
        return Response({'message': 'success'})
    

    从django.url导入路径

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

    并在主应用程序中包含图形url网址.py

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

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

    完全回溯

    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 回复  |  直到 7 年前
        1
  •  1
  •   Enthusiast Martin    7 年前

    路由器的注册函数需要一个viewset,所以不可能以您想要的方式包含图形的url。

    get_extra_actions 是ViewSet类的方法,并且 include 返回元组。因此,您可以忽略此错误,因为它在构建路由时尝试对include返回的元组调用此函数。

    改为像这样包含图形URL:

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