姜戈。 ''The `actions` argument must be provided when calling `.as_view()` '' 当我尝试允许 DELETE、PUT 等时

Django. ''The `actions` argument must be provided when calling `.as_view()` '' when I try to allow DELETE, PUT, ETC

我必须允许从前端删除和更新我的某些模型对象的请求。我想删除数据库中的实例和适当的行。

我尝试使用 DRF 教程 (https://www.django-rest-framework.org/tutorial/6-viewsets-and-routers/) 中的信息和其他一些示例。 我知道如果我使用 ViewSet 我必须允许一些操作和使用行。我像 DRF 教程中那样使用装饰器。

有我的view.py

class DualFcaPlanUseViewSet(viewsets.ModelViewSet):

    authentication_classes = (CsrfExemptSessionAuthentication,)
    def get_queryset(self): 
        user = self.request.user
        return FcaPlanUse.objects.filter(id_fca__num_of_agree__renters_id__user_key = user)
    def get_serializer_class(self):
        if self.request.method == 'GET':
            return FcaPlanUseSerializer
        if self.request.method == 'POST':
            return FcaPlanUsePOSTSerializer
    @action(detail=True, renderer_classes=[renderers.StaticHTMLRenderer]) 
    def highlight(self, request, *args, **kwargs):
        fcaplanuse = self.get_object()
        return Response(fcaplanuse.highlighted)
    def perform_create(self, serializer):
        serializer.save(owner=self.request.user)

我在应用程序中的操作 urls.py

from django.conf.urls import url
from rest_framework import renderers
from . import views
from cutarea.views import DualFcaPlanUseViewSet

fcaplanuse_list = DualFcaPlanUseViewSet.as_view({
    'get': 'list',
    'post': 'create'
})
fcaplanuse_detail = DualFcaPlanUseViewSet.as_view({
    'get': 'retrieve',
    'put': 'update',
    'patch': 'partial_update',
    'delete': 'destroy'
})
fcaplanuse_highlight = DualFcaPlanUseViewSet.as_view({
    'get': 'highlight'
}, renderer_classes=[renderers.StaticHTMLRenderer])

所以我项目的一部分 urls.py

from cutarea.views import *
#...
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r'cutarea', DualFcaPlanUseViewSet.as_view(), base_name='cutareadel')
#...

urlpatterns = [
    #...
    url(r'^api/', include(router.urls)),
]

所以结果:TypeError: The `actions` argument must be provided when calling `.as_view()` on a ViewSet. For example `.as_view({'get': 'list'})

如果我设置了一些像示例中那样的操作被终端抛出:router.register(r'cutarea', DualFcaPlanUseViewSet.as_view('destroy': 'delete'), base_name='cutareadel')我遇到语法错误... 我想了解视图集如何与路由器一起工作,以及允许额外的 HTTP 方法(删除、更新等)的好方法

UPD 如果使用这个

router.register(r'cutarea', DualFcaPlanUseViewSet, base_name='cutareadel')``` The error is solved. But DELETE method not allowed. What is wrong?

注册 ViewSet 时没有使用 as_view:

from cutarea.views import *
#...
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r'cutarea', DualFcaPlanUseViewSet, basename='cutareadel')
#...

urlpatterns = [
    #...
    url(r'^api/', include(router.urls)),
]

编辑:使用 basename 而不是 base_name 感谢@greg-schmit 的指点。

您必须将 {'get': 'list'} 添加到

router.register(r'cutarea', DualFcaPlanUseViewSet.as_view(), base_name='cutareadel')

router.register(r'cutarea', DualFcaPlanUseViewSet.as_view({'get': 'list'}), base_name='cutareadel')