Django 视图集没有属性“get_extra_actions”
Posted
技术标签:
【中文标题】Django 视图集没有属性“get_extra_actions”【英文标题】:Django viewset has not attribute 'get_extra_actions' 【发布时间】:2018-09-18 03:39:22 【问题描述】:我第一次使用 Django,我正在尝试构建一个 API,我正在遵循一些教程和示例,它工作正常,但我现在在安装所有要求后在 Raspberry Pi 中运行该项目并且项目失败并出现以下错误:
Performing system checks...
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0xb547adb0>
Traceback (most recent call last):
File "/home/pi/.local/lib/python3.5/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/home/pi/.local/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 120, in inner_run
self.check(display_num_errors=True)
File "/home/pi/.local/lib/python3.5/site-packages/django/core/management/base.py", line 364, in check
include_deployment_checks=include_deployment_checks,
File "/home/pi/.local/lib/python3.5/site-packages/django/core/management/base.py", line 351, in _run_checks
return checks.run_checks(**kwargs)
File "/home/pi/.local/lib/python3.5/site-packages/django/core/checks/registry.py", line 73, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/pi/.local/lib/python3.5/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/home/pi/.local/lib/python3.5/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/home/pi/.local/lib/python3.5/site-packages/django/urls/resolvers.py", line 397, in check
for pattern in self.url_patterns:
File "/home/pi/.local/lib/python3.5/site-packages/django/utils/functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/pi/.local/lib/python3.5/site-packages/django/urls/resolvers.py", line 536, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/pi/.local/lib/python3.5/site-packages/django/utils/functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/pi/.local/lib/python3.5/site-packages/django/urls/resolvers.py", line 529, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 673, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/pi/Projects/open***-monitor/open***monitor/urls.py", line 24, in <module>
url(r'^api/', include('open***monitor.api.urls')),
File "/home/pi/.local/lib/python3.5/site-packages/django/urls/conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 673, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/pi/Projects/open***-monitor/open***monitor/api/urls.py", line 16, in <module>
urlpatterns += router.urls
File "/home/pi/.local/lib/python3.5/site-packages/rest_framework/routers.py", line 101, in urls
self._urls = self.get_urls()
File "/home/pi/.local/lib/python3.5/site-packages/rest_framework/routers.py", line 363, in get_urls
urls = super(DefaultRouter, self).get_urls()
File "/home/pi/.local/lib/python3.5/site-packages/rest_framework/routers.py", line 261, in get_urls
routes = self.get_routes(viewset)
File "/home/pi/.local/lib/python3.5/site-packages/rest_framework/routers.py", line 176, in get_routes
extra_actions = viewset.get_extra_actions()
AttributeError: type object 'SessionViewSet' has no attribute 'get_extra_actions'
我的views.py有以下代码:
from django.shortcuts import render
from rest_framework import viewsets
from .models import Session
from .serializers import SessionSerializer
from rest_framework.views import APIView, Response
class SessionViewSet(APIView):
queryset = Session.objects.all()
serializer_class = SessionSerializer
def get(self, request, format=None):
return Response("test")
我真的不知道为什么可以在我的笔记本电脑上工作,但它却不能在我的 Raspberry Pi 上工作。
有人或任何人知道为什么会发生这种情况吗?
非常感谢!
编辑:
这是我的 urls.py
from django.conf.urls import url
from rest_framework import routers
from open***monitor.api.views import SessionViewSet
router = routers.DefaultRouter()
router.register(r'sessions', SessionViewSet)
urlpatterns = [
url(r'sessions', SessionViewSet.as_view()),
url(r'^docs/', schema_view),
]
urlpatterns += router.urls
【问题讨论】:
【参考方案1】:注意不要为视图集类和模型类使用相同的名称。这就是我自己错误的原因。查看我所做的示例
# inside member/views.py
from member.models import Member
# inheriting from model viewset but called Member
class Member(viewsets.ModelViewSet):
queryset = Member.objects.all()
...
# inside urls.py
from member.views import Member
router = routers.DefaultRouter()
router.register(r'member', Member)
现在这里的错误是它导入了成员模型而不是视图集,但它们的名称相同
【讨论】:
【参考方案2】:在我的例子中,我所做的是我从位于 rest_framework 模块中的 viewsets.Viewset 继承了我的视图,此外,我在注册期间在 router.register() 函数中添加了 basename = your_name 参数。
视图看起来像:
class SessionViewSet(viewsets.ViewSet):
queryset = Session.objects.all()
serializer_class = SessionSerializer
def get(self, request, format=None):
return Response("test")
路由器注册看起来像:
router.register(r'your_app_name', YourModelNameView, basename='your_app_name')
【讨论】:
错字,viewsets.ViewSet【参考方案3】:为:
djangorestframework==3.11.0
Django==2.2.9
您需要更改class SessionViewSet(APIView):
到:
from rest_framework import mixins, viewsets
class SessionViewSet(mixins.ListModelMixin,
viewsets.GenericViewSet):
让它工作。 DRF 的内部结构发生了一些变化,其他解决方案不再适用。
【讨论】:
截至 2020 年 4 月,这是最新的答案。谢谢!【参考方案4】:在views.py中,你的viewset必须继承viewset并在你的viewset中使用它试试下面的代码:
class SessionViewSet(viewsets.ModelViewSet):
queryset = Session.objects.all()
serializer_class = SessionSerializer
def get(self, request, format=None):
return Response("test")
【讨论】:
【参考方案5】:在 Django Rest Framework v3.8 之前,您可以直接向路由器注册 APIView
。我广泛地这样做是为了为一些 非常 自定义 API 端点获得一个很好的整理(和版本化)自动文档 API。如果再有选择,我可能会以更标准的方式编写整个内容,但这不是每个人的选择。
但在深入研究错误之后,事实证明您可以通过为路由器提供所需的内容并添加一个虚拟的 get_extra_actions
类方法来修补问题。
class MyAPIView(APIView):
@classmethod
def get_extra_actions(cls):
return []
#...
我并不是说这很好,但它现在有效。 我拿回了我的文档,并成功升级到 DRFv3.8。
【讨论】:
嗨!我将 DRF 3.10.2 与 Django 2.2.4 一起使用。添加此存根方法会导致路由器忽略此路由:(【参考方案6】:你称它为视图集,但这并不能使它成为一个;您从 APIView 继承,这是一个独立的通用视图,而不是视图集。
视图集需要从viewsets.ViewSet 继承。
【讨论】:
我不明白你的意思。该教程创建了视图集和独立的 APIView;您似乎混淆了这两种方法。 是的,该教程创建了视图集和独立的 APIView,我只创建了一个 APIView 来拥有一个自定义视图,并且只有 GET 端点而不是 CRUD,这在我的笔记本电脑上工作,我的意思是,如果我在笔记本电脑上运行该项目,它可以正常工作,但我试图在我的树莓派上运行相同的项目,但我得到了那个错误。我刚刚尝试将 APIView 更改为 Raspberry Pi 上的 ViewSet 并且它可以工作,但我想使用 APIView 来拥有一个只有 GET 端点的端点。 你需要显示你的urls.py;从教程中可以看出,视图集和独立视图在 url 中的引用方式不同。 谢谢丹尼尔,我刚刚编辑了我的问题并添加了我的 urls.py 您注册了该视图两次。独立视图不需要路由器。 (注意,如果它不是一个视图集,你真的不应该首先将它称为视图集。)以上是关于Django 视图集没有属性“get_extra_actions”的主要内容,如果未能解决你的问题,请参考以下文章
python Django Rest_Framework框架 视图集与路由Routers详解(图文并茂版)
python Django Rest_Framework框架 视图集与路由Routers详解(图文并茂版)
python Django Rest_Framework框架 视图集与路由Routers详解(图文并茂版)