【问题标题】:Delete/destroy method in djangodjango中的删除/销毁方法
【发布时间】:2019-09-12 20:17:39
【问题描述】:

我有一个通用的 api 视图,我想将它用于放置、删除(并最终更新)某个模型的记录,但我对在 Django 中删除记录的最佳实践感到相当困惑。我应该使用内置的删除方法,还是我自己定义?我是否将其作为 GenericAPIView 上的 DELETE 或“销毁”方法处理。我不想只允许任何人删除记录,所以我需要首先验证他们是创建记录的同一用户。在某些帐户中,听起来 Django 允许您仅使用身份验证和 id 来删除记录。如果为 true,如何禁用此行为?

感谢您提供有关这些不同问题的任何代码或指导。

前端.js

const deleteRow = (id) => {
alert(id)

fetch(`${SERVER_URL}/api/v1/requirements/related_files/${id}`, {
  method: 'DELETE',
  credentials: 'include',
  headers: {
    Accept: 'application/json, text/plain, */*',
    'Content-Type': 'application/json',
    Authorization: `Token ${token}`,
  },

views.py

class CommentsView(GenericAPIView):
  authentication_classes = (TokenAuthentication,)
  serializer_class = CommentsSerializer

  def post(self, request):
    request.data['user'] = request.user.id
    comment = CommentsSerializer(data=request.data)

    if comment.is_valid():
        comment.save()
        return Response(comment.data, status=status.HTTP_201_CREATED)

    return Response(comment.errors, status=status.HTTP_400_BAD_REQUEST)

  def delete(self,request):
    ???? what do I do here ????

【问题讨论】:

标签: python django django-models django-rest-framework django-views


【解决方案1】:

URL 应该包含您想要删除它的对象。我们假设urls.py 类似于:

url(r'^/api/v1/requirements/related_files/(?P<comment_id>[0-9]+)/$', views.CommentsView.as_view())

然后在delete 部分我们只需要comment_id:

class CommentsView(GenericAPIView):
  authentication_classes = (TokenAuthentication,)
  serializer_class = CommentsSerializer

  def post(self, request):
    request.data['user'] = request.user.id
    comment = CommentsSerializer(data=request.data)

    if comment.is_valid():
        comment.save()
        return Response(comment.data, status=status.HTTP_201_CREATED)

    return Response(comment.errors, status=status.HTTP_400_BAD_REQUEST)

  def delete(self,request):
    comment_id = self.kwargs["comment_id"]
    comment = get_object_or_404(Comment, id=comment_id)
    comment.delete()
    return Response(status=204)

【讨论】:

    【解决方案2】:

    您还可以使用 mixin,即 UpdateModelMixin 和 DestroyModelMixin 以及 GenericViewSet。

    class CommentsViewSet(UpdateModelMixin, DestroyModelMixin, GenericViewSet):
        authentication_classes = (TokenAuthentication,)
        serializer = CommentsSerializer
        Model = Comments // Write your model name here
        queryset = Comments.objects.all()
    

    那么你的 url 将如下所示,因为你使用 ViewSet 你应该使用路由器。

        from rest_framework.routers import DefaultRouter
        router = DefaultRouter()
        router.register(r'^requirements/related_files/', views.CommentsViewSet)
        urlpatterns = {
            path("/api/v1/", include(router.urls))
                  }
    

    【讨论】:

      【解决方案3】:

      使用内置行为很好,只是子类rest_framework.viewsets.ModelViewSet - 它带有所有常用的创建/更新/删除功能。如果你想保护删除,添加你自己的权限类。

      from rest_framework.permissions import BasePermission
      
      class OnlyOwnerDeletePermission(BasePermission):
          def has_object_permission(self, request, view, obj):
              if request.method == "DELETE":
                  return request.user.id == obj.user_id # prevent fetching whole user model
              return True # anyone can do any other action
      

      【讨论】:

        猜你喜欢
        • 2012-03-01
        • 2015-06-13
        • 1970-01-01
        • 2020-07-05
        • 2014-05-10
        • 1970-01-01
        • 2013-10-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多