Django Rest:为/resource删除/

2024-09-27 21:26:57 发布

您现在位置:Python中文网/ 问答频道 /正文

有谁能告诉我们如何在资源中为DELETE设置一条最佳路径吗?在

通常,如果我使用GenericAPIView,我可以继承mixins.DestroyModelMixin。在

示例

然后我可以得到/resource/{id}的终点DELETE

Q:如何为/resource/DELETE提供端点?在

谢谢!在


Tags: 路径id示例资源端点deletemixinsresource
2条回答

找到了办法。在ViewSet类中添加delete()方法,例如

Class MyViewSet(GenericViewSet):
    def delete(self, request, *args, **kwargs):
      ...

然后我可以:

^{pr2}$

如您所述,只需将DestroyModelMixin添加到您的详细视图中。然后删除具有以下请求的资源:

curl -X DELETE "http://127.0.0.1:8000/resouce/1/"

有关DELETEhttp方法here的更多信息。在

DELETE is pretty easy to understand. It is used to delete a resource identified by a URI.

On successful deletion, return HTTP status 200 (OK) along with a response body, perhaps the representation of the deleted item (often demands too much bandwidth), or a wrapped response (see Return Values below). Either that or return HTTP status 204 (NO CONTENT) with no response body. In other words, a 204 status with no body, or the JSEND-style response and HTTP status 200 are the recommended responses.

HTTP-spec-wise, DELETE operations are idempotent. If you DELETE a resource, it's removed. Repeatedly calling DELETE on that resource ends up the same: the resource is gone. If calling DELETE say, decrements a counter (within the resource), the DELETE call is no longer idempotent. As mentioned previously, usage statistics and measurements may be updated while still considering the service idempotent as long as no resource data is changed. Using POST for non-idempotent resource requests is recommended.

There is a caveat about DELETE idempotence, however. Calling DELETE on a resource a second time will often return a 404 (NOT FOUND) since it was already removed and therefore is no longer findable. This, by some opinions, makes DELETE operations no longer idempotent, however, the end-state of the resource is the same. Returning a 404 is acceptable and communicates accurately the status of the call.

Examples:

相关问题 更多 >

    热门问题