简体   繁体   English

Django Rest:删除/ resource /

[英]Django Rest: DELETE for /resource/

Can anyone please shine some light on how best to set up a route for DELETE at a resource? 任何人都可以就如何最好地在资源上设置DELETE的路线DELETE吗?

Typically, if I use a GenericAPIView , I can inherit the mixins.DestroyModelMixin . 通常,如果使用GenericAPIView ,则可以继承mixins.DestroyModelMixin

example : 例如

I could then have end point /resource/{id} for DELETE 然后,我可以将端点/resource/{id}用于DELETE

Q : How would I have an endpoint for /resource/ for DELETE ? :如何为/resource/提供DELETE的终结点?

Thanks! 谢谢!

Like you mentioned, simply add the DestroyModelMixin to your detail view. 就像您提到的,只需将DestroyModelMixin添加到您的详细信息视图中。 Then delete resources with requests like: 然后使用以下请求删除资源:

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

More info about the DELETE http method here . 有关DELETE http方法的更多信息,请DELETE 此处

DELETE is pretty easy to understand. 删除非常容易理解。 It is used to delete a resource identified by a URI. 它用于删除 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). 成功删除后,返回HTTP状态200(OK)以及响应正文,可能是已删除项目的表示形式(通常需要太多带宽)或已包装的响应(请参见下面的返回值)。 Either that or return HTTP status 204 (NO CONTENT) with no response body. 要么返回HTTP状态204(无内容),就没有响应主体。 In other words, a 204 status with no body, or the JSEND-style response and HTTP status 200 are the recommended responses. 换句话说,建议使用无主体的204状态或JSEND样式的响应以及HTTP状态200。

HTTP-spec-wise, DELETE operations are idempotent. 在HTTP规范方面,DELETE操作是幂等的。 If you DELETE a resource, it's removed. 如果删除资源,则将其删除。 Repeatedly calling DELETE on that resource ends up the same: the resource is gone. 对该资源重复调用DELETE的结果相同:该资源已消失。 If calling DELETE say, decrements a counter (within the resource), the DELETE call is no longer idempotent. 如果调用DELETE说,减少一个计数器(在资源内),则DELETE调用不再是幂等的。 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. 建议将POST用于非幂等资源请求。

There is a caveat about DELETE idempotence, however. 但是,关于DELETE幂等性有一个警告。 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. 第二次在资源上调用DELETE通常会返回404(找不到),因为它已被删除,因此不再可以找到。 This, by some opinions, makes DELETE operations no longer idempotent, however, the end-state of the resource is the same. 从某些观点来看,这使得DELETE操作不再是幂等的,但是资源的最终状态是相同的。 Returning a 404 is acceptable and communicates accurately the status of the call. 返回404是可以接受的,并且可以准确传达通话状态。

Examples: 例子:

Found out the way to do this. 找到了解决方法。 Add a delete() method at the ViewSet class, eg 在ViewSet类中添加delete()方法,例如

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

Then I could do: 然后我可以做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM