尝试检索单个资源时,DRF ViewSet 中不允许方法 GET
Method GET not allowed in DRF ViewSet when trying to retrieve single resource
我是新手 Python 和 Django。我按如下方式创建了 ViewSet:
api/views.py
class UserDetails(ViewSet):
"""
CREATE, SELECT, UPDATE OR DELETE
"""
def retrive(self, request, pk):
user = self.get_object(pk)
print(user.query)
user = TestSerializer(user)
return Response(user.data)
def list(self, request):
users = TestTB.objects.all()
print(users.query)
serializer = TestSerializer(users, many=True)
return Response(serializer.data)
def create(self, request):
serializer = TestSerializer(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 update(self, request, pk):
user = self.get_object(pk)
serializer = TestSerializer(user, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def destroy(self, request, pk):
user = self.get_object(pk)
user.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
def get_object(self, pk):
try:
return TestTB.objects.get(pk=pk)
except TestTB.DoesNotExist:
raise Http404
api/urls.py
router = routers.DefaultRouter()
router.register(r'users', UserDetails, base_name='user-details')
urlpatterns = router.urls
这适用于 POST 创建新资源的请求、获取所有资源的 GET 请求、更新资源的 PUT 请求和删除资源的 DELETE 请求。但是我怎样才能检索单个资源呢?当我提出 http://127.0.0.1:8000/api/users/1/
之类的请求时,它显示错误 -
{
"detail": "Method \"GET\" not allowed."
}
这意味着永远不会调用 UserDetails 中的 retrieve() 方法。我知道我遗漏了一些东西,但无法弄清楚是什么。
将 retrive
更改为 retrieve
。按照@Linovia 的建议从评论中做出回答。
我是新手 Python 和 Django。我按如下方式创建了 ViewSet:
api/views.py
class UserDetails(ViewSet):
"""
CREATE, SELECT, UPDATE OR DELETE
"""
def retrive(self, request, pk):
user = self.get_object(pk)
print(user.query)
user = TestSerializer(user)
return Response(user.data)
def list(self, request):
users = TestTB.objects.all()
print(users.query)
serializer = TestSerializer(users, many=True)
return Response(serializer.data)
def create(self, request):
serializer = TestSerializer(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 update(self, request, pk):
user = self.get_object(pk)
serializer = TestSerializer(user, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def destroy(self, request, pk):
user = self.get_object(pk)
user.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
def get_object(self, pk):
try:
return TestTB.objects.get(pk=pk)
except TestTB.DoesNotExist:
raise Http404
api/urls.py
router = routers.DefaultRouter()
router.register(r'users', UserDetails, base_name='user-details')
urlpatterns = router.urls
这适用于 POST 创建新资源的请求、获取所有资源的 GET 请求、更新资源的 PUT 请求和删除资源的 DELETE 请求。但是我怎样才能检索单个资源呢?当我提出 http://127.0.0.1:8000/api/users/1/
之类的请求时,它显示错误 -
{ "detail": "Method \"GET\" not allowed." }
这意味着永远不会调用 UserDetails 中的 retrieve() 方法。我知道我遗漏了一些东西,但无法弄清楚是什么。
将 retrive
更改为 retrieve
。按照@Linovia 的建议从评论中做出回答。