1024programmer Java Django. When I try to allow DELETE, PUT, ETC, the `actions parameter’ must be provided when calling `.as_view()`

Django. When I try to allow DELETE, PUT, ETC, the `actions parameter’ must be provided when calling `.as_view()`

I have to allow requests to delete and update certain model objects from the front. I wish to delete the instance and appropriate row in the database.

I tried using the information from the DRF tutorial (https://www.django-rest-framework.org/tutorial/6-viewsets-and-routers/), along with some other examples. I know if using ViewSet I have to allow some operations and usage rows. I’m using decorators in the DRF tutorial.

There is my 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)
 

My operations in application 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])

 

So part of my project 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)),
 ]
 

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

If I set some examples, like the action being thrown by the terminal: router.register(r'cutarea', DualFcaPlanUseViewSet.as_view('destroy': 'delete'), base_name='cutareadel') I’m getting a syntax error… I’d like to understand how viewsets work with router and is a good way to allow additional HTTP methods (delete, update, etc.).

UPD,
If you use this

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

Linovia..
5

as_viewDo not use ViewSet when registering:

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)),
 ]
 

EDIT: Use basename instead of base_name Thanks to @greg-schmit for pointing it out.

1> Linovia..:


as_view does not use ViewSet when registering:

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)),
 ]
 

EDIT: Use basename instead of base_name Thanks to @greg-schmit for pointing it out.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/django-when-i-try-to-allow-delete-put-etc-the-actions-parameter-must-be-provided-when-calling-as_view/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

The latest and most comprehensive programming knowledge, all in 1024programmer.com

© 2023 1024programmer - Encyclopedia of Programming Field
Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us