Python Django 视图集没有属性“get_extra_actions”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49721089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:13:19  来源:igfitidea点击:

Django viewset has not attribute 'get_extra_actions'

pythondjangopython-3.xdjango-rest-framework

提问by alexca

I am working with Django for a first time and I'm trying to build an API and I am following some tutorials and examples and it works right, but I am running the project now in a Raspberry Pi after install all the requirements and the project is failing with the following error:

我第一次与 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/openvpn-monitor/openvpnmonitor/urls.py", line 24, in <module>
    url(r'^api/', include('openvpnmonitor.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/openvpn-monitor/openvpnmonitor/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'

My views.py has the following code:

我的 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")

I really don't know why is working on my laptop but it is not working on my Raspberry Pi.

我真的不知道为什么在我的笔记本电脑上工作,但它在我的 Raspberry Pi 上不起作用。

Has this happened to someone or anyone knows why is happening this?

这是否发生在某人身上或任何人都知道为什么会发生这种情况?

Thank you so much!

非常感谢!

Edit:

编辑:

Here is my urls.py

这是我的 urls.py

from django.conf.urls import url
from rest_framework import routers
from openvpnmonitor.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

回答by Daniel Roseman

You've called it a viewset, but that doesn't make it one; you inherit from APIView which is a standalone generic view, not a viewset.

您将其称为视图集,但这并没有使它成为一个视图集;你从 APIView 继承,它是一个独立的通用视图,而不是一个视图集。

A viewset needs to inherit from viewsets.ViewSet.

视图集需要继承自 viewsets.ViewSet。

回答by Oli

Before Django Rest Framework v3.8 you couldregister an APIViewdirectly with a router. I did this extensively to gain a nice collated (and versioned) auto-documenting API for some verycustom API endpoints. Given the choice again, I would probably write the whole thing a more standard way, but that isn't an option for everybody.

在 Django Rest Framework v3.8 之前,您可以APIView直接向路由器注册。我这样做是为了为一些非常自定义的 API 端点获得一个很好的整理(和版本化)自动文档化 API 。如果再次选择,我可能会以更标准的方式写整个事情,但这不是每个人的选择。

But after digging into the error, it turns out you can just patch over the problem by giving the router what it wants and adding a dummy get_extra_actionsclassmethod.

但是在深入研究错误之后,结果证明您可以通过为路由器提供所需的内容并添加虚拟get_extra_actions类方法来解决问题。

class MyAPIView(APIView):

    @classmethod
    def get_extra_actions(cls):
        return []

#...

I'm not saying this is good, but it works for now.
I've got my documentation back and I've managed to upgrade to DRFv3.8.

我不是说这很好,但它现在有效。
我已取回文档,并且已成功升级到 DRFv3.8。

回答by boatcoder

For:

为了:

djangorestframework==3.11.0
Django==2.2.9

You need to change class SessionViewSet(APIView):to:

您需要更改class SessionViewSet(APIView):为:

from rest_framework import mixins, viewsets

class SessionViewSet(mixins.ListModelMixin,
                     viewsets.GenericViewSet):

To get it to work. The internals of DRF have changed a bit and the other solutions won't cut it any longer.

让它工作。DRF 的内部结构发生了一些变化,其他解决方案不会再削减它。

回答by Ehsan Ahmadi

In views.py, your viewset have to inherit from viewset and use it in your viewset try code below:

在 views.py 中,您的视图集必须从视图集继承并在您的视图集中使用它,请尝试以下代码:

class SessionViewSet(viewsets.ModelViewSet):
    queryset = Session.objects.all()
    serializer_class = SessionSerializer

    def get(self, request, format=None):
        return Response("test")

回答by Varun Sharma

In my case, what I did was I inherited my view from viewsets.Viewset which is in rest_framework module and additionally, I added the basename = your_name argument in the router.register() function during registration.

就我而言,我所做的是从rest_framework 模块中的viewset.Viewset 继承了我的视图,另外,我在注册期间在router.register() 函数中添加了basename = your_name 参数。

The view would look something like:

该视图将类似于:

class SessionViewSet(viewsets.ViewSet):
    queryset = Session.objects.all()
    serializer_class = SessionSerializer

    def get(self, request, format=None):
        return Response("test")

The router registeration would look something like:

路由器注册看起来像:

router.register(r'your_app_name', YourModelNameView, basename='your_app_name')