python - Method GET not allowed in DRF ViewSet when trying to retrieve single resource -
i new python , django. have created viewset follows:
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
this works fine post request create new resource, request resources, put request update resource , delete request delete resource. how can retrieve single resource? when make request http://127.0.0.1:8000/api/users/1/
shows error -
{ "detail": "method \"get\" not allowed." }
it means retrieve() method in userdetails never called. know missing something, not able figure out what.
change retrive
retrieve
. made answer out of comment, suggested @linovia.
Comments
Post a Comment