6

I have to allow delete and update requests from front for my objects of some model. I wish to delete instance and appropriate row in DB.

I tryed to use info from DRF tutorials (https://www.django-rest-framework.org/tutorial/6-viewsets-and-routers/) , some other examples. I understand if I using ViewSet I have to allow some actions and using rows. I use decorator like in 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 actions in app 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 a 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)),
]

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

If I set some action like in example was throwen by terminal: router.register(r'cutarea', DualFcaPlanUseViewSet.as_view('destroy': 'delete'), base_name='cutareadel') I face syntax error... I want to understand how viewset works with routers, and wich good way to allow extra HTTP methods (delete, update etc.)

UPD if use this

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

1 Answers1

17

You don't use as_view when registering a ViewSet:

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: using basename instead of base_name thanks to @greg-schmit for pointing it.

Linovia
  • 15,428
  • 4
  • 28
  • 34
  • ,Y. All looks like you define In DRF example . I used my variant cause tried to find how to allow delete(https://stackoverflow.com/questions/46053922/delete-method-on-django-rest-framework-modelviewset), But if ```router.register(r'cutarea', DualFcaPlanUseViewSet, base_name='cutareadel')``` api/cutarea/ allows only: GET, POST, HEAD, OPTIONS . No DELETE. Request from frontside doesn't work too. – Tyomik_mnemonic May 15 '19 at 12:31
  • 1
    I can't understand what you are trying to do. Your question here was about ''The `actions` argument must be provided when calling `.as_view()` ''. If you have another point, please open a different question – Linovia May 15 '19 at 12:37
  • 1
    Shouldn't it be `basename` rather than `base_name`? – Greg Schmit Oct 21 '19 at 17:04
  • `base_name` was the historical name. DRF is transitioning to `basename`. thanks for pointing out. – Linovia Oct 21 '19 at 18:31