Lin
Lin

Reputation: 11

get and delete in Django framework

i want to build a ModelViewSet class that recive an id from url for instance

localhost/id

and based on that id i can either show the object with matching id or delete the object but im having trouble passing the id in the url my view is like this:

class delete_or_show_View(viewsets.ModelViewSet):
    serializer_class = ObjectSerializer
    permission_classes = [permissions.IsAuthenticated]
    http_method_names = ['get', 'delete']

    def get_queryset(self,mid):
        #Show the object


    def destroy(self, mid):
        #delete the object

and my url is like this

router.register('(?P<object_id>\d+)', views.delete_or_show_View, basename='do_stuff')

Im getting errors for missing aruguments or that the delete method is not allowed please if someone can guide me how i can do this properly and explain it will be great thank you

Upvotes: -1

Views: 142

Answers (1)

Sarath R
Sarath R

Reputation: 156

class DeleteOrShowView(viewsets.ModelViewSet):
    serializer_class = ObjectSerializer
    permission_classes = [permissions.IsAuthenticated]
    queryset = Model.objects.all()
    http_method_names = ['get', 'delete']

then update your urls.py as

router = DefaultRouter()
router.register('show-delete', views.DeleteOrShowView, basename='do_stuff')

now you can just pass along with this url when hitting this api show-delete/<id>/

Upvotes: 2

Related Questions