我创建了一个基于函数的视图,因为它不需要直接的模型查询集。
graph_data / views.py
@api_view(http_method_names=['GET'])
def my_func(request):
print(request)
return Response({'message': 'success'})
并在 graph_data / urls.py
中从django.urls导入路径
从graph_data.views导入my_func
app_name = 'graph_data'
urlpatterns = [
path('my_func/<contact_id>', my_func, name='my_func')
]
并在主应用程序urls.py中包含了图形url
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'
答案 0 :(得分:0)
路由器的注册功能采用了一个视图集,因此无法以您希望的方式包含图形的url。
get_extra_actions
是ViewSet类的方法,include
返回一个元组。因此,您会遇到这个错误,因为它会在构建路线时尝试在包含返回的元组上调用此函数。
改为包含这样的图形网址:
urlpatterns = [
path(r'graph/', include(('graph_data.urls','graph_data'), namespace='graph_data')),
]