gpt4 book ai didi

python - 如何使用 Django Rest Framework 删除对象

转载 作者:太空狗 更新时间:2023-10-29 17:39:39 24 4
gpt4 key购买 nike

我正在尝试使用 Django Rest Framework 为我的事件计划应用程序编写 RESTful API,但在使用不需要 GET HTTP 方法的 View 时遇到了一些麻烦。我已经阅读了 DRF 网站上的教程。根据我阅读教程和 Django 站点上基于类的 View 文档后的理解,如果有这样一个基于类的 View (取自 DRF 教程)

class SnippetDetail(APIView):
"""
Retrieve, update or delete a snippet instance.
"""
def get_object(self, pk):
try:
return Snippet.objects.get(pk=pk)
except Snippet.DoesNotExist:
raise Http404

def get(self, request, pk, format=None):
snippet = self.get_object(pk)
serializer = SnippetSerializer(snippet)
return Response(serializer.data)

def put(self, request, pk, format=None):
snippet = self.get_object(pk)
serializer = SnippetSerializer(snippet, data=request.DATA)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

def delete(self, request, pk, format=None):
snippet = self.get_object(pk)
snippet.delete()
return Response(status=status.HTTP_204_NO_CONTENT)

View 中不同的方法对应不同的HTTP Request方法。因此,如果我有 www.foo.com/bar,它会根据发送到该地址的请求方法做两件不同的事情。所以这意味着我不必指定任何其他内容,因为执行的函数是根据发送 URL 的方法确定的。这是正确的吗?

我有这个 View ,我试图根据 DRF 站点上的示例建模

class EventDetail(APIView):

"""
Retrieve, update or delete a event instance.
"""

def get_object(self, pk):
try:
return Event.objects.get(pk=pk)
except Event.DoesNotExist:
raise Http404

def get(self, request, pk, format=None):
event = self.get_object(pk)
serializer = EventSerializer(event)
return Response(serializer.data)

def post(self, request, format=None):
serializer = EventSerializer(data=request.DATA)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

# def put(self, request, pk, format=None):
# event = self.get_object(pk)
# serializer = EventSerializer(event, data=request.DATA)
# if serializer.is_valid():
# serializer.save()
# return Response(serializer.data)
# return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

def delete(self, request, pk, format=None):
event = self.get_object(pk)
event.delete()
return Response(status=status.HTTP_204_NO_CONTENT

哪些映射到这些 URL

urlpatterns = patterns('',

# Get event
url(r'^(?P<pk>\d+)/$', views.EventDetail.as_view(),
name='create_events'),
# list all events
url(r'^list/$', views.EventList.as_view(),
name='list_events'),
# url(r'^update$/(?P<pk>\d+)', #update event),
url(r'^create/$', views.EventDetail.as_view(),
name='create_events'),
# delete event
url(r'^delete$/(?P<pk>\d+)',
views.EventDetail.as_view(), name='delete_event'),

)

我正在尝试通过此命令使用 CURL 进行测试(如此处建议的 DELETE using CURL with encoded URL )

curl -X DELETE "http://127.0.0.1:8000/events/delete/1"

这似乎做了它应该做的事情:

[18/Oct/2014 22:41:27] "DELETE /events/delete/1 HTTP/1.1" 404 2707

但实际记录并没有从我的数据库中删除

这里有什么我忘记做的事情来让它正常工作吗?

最佳答案

你是多余的。 HTTP方法已经是DELETE , 所以没有 /events/delete在网址中。试试这个:

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

默认情况下,DRF 的路由器在 /event/<pk> 处创建详细的 url。还有你GET , PUT , POSTDELETE它们分别检索、更新、创建和删除。

关于python - 如何使用 Django Rest Framework 删除对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26445450/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com