在Django框架中获取和删除

j2qf4p5b  于 2022-12-01  发布在  Go
关注(0)|答案(1)|浏览(137)

我想生成一个ModelViewSet类,该类从示例url接收ID
本地主机/id
并且基于该ID,我可以显示具有匹配ID的对象,或者删除该对象,但是我在URL中传递ID时遇到麻烦。我的观点是这样的:

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

我的网址是这样的

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

我得到错误的缺失aruguments或删除方法是不允许的,请如果有人可以指导我如何可以正确地做这件事,并解释它将是伟大的,谢谢

htrmnn0y

htrmnn0y1#

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

然后将您的urls.py更新为

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

现在你可以在点击这个api show-delete/<id>/时传递这个url

相关问题