python - DRF request handler is throwing exception: retrieve() got an unexpected keyword argument 'pk' -
i'm trying retrieve model instance in django rest framework 3.6.3 @ /path/to/api/widget/1/
, 1 being primary key of widget. it's giving me:
retrieve() got unexpected keyword argument 'pk'
from trace think i'm doing cause dispatch
throw exception when instantiating handler (from views.py, source here). don't understand i'm doing wrong through when examine source. here's trace:
traceback:
file "/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner 41. response = get_response(request) file "/lib/python3.5/site-packages/django/core/handlers/base.py" in _legacy_get_response 249. response = self._get_response(request) file "/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response 187. response = self.process_exception_by_middleware(e, request) file "/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response 185. response = wrapped_callback(request, *callback_args, **callback_kwargs) file "/lib/python3.5/site-packages/django/views/decorators/csrf.py" in wrapped_view 58. return view_func(*args, **kwargs) file "/lib/python3.5/site-packages/rest_framework/viewsets.py" in view 86. return self.dispatch(request, *args, **kwargs) file "/lib/python3.5/site-packages/rest_framework/views.py" in dispatch 489. response = self.handle_exception(exc) file "/lib/python3.5/site-packages/rest_framework/views.py" in handle_exception 449. self.raise_uncaught_exception(exc) file "/lib/python3.5/site-packages/rest_framework/views.py" in dispatch 486. response = handler(request, *args, **kwargs) exception type: typeerror @ /path/to/api/widget/1/ exception value: retrieve() got unexpected keyword argument 'pk'
here's view viewset
:
def retrieve(self, request, some_lookup_field=none): instance = self.get_object(pk) # handle multi-tenant access control if not instance: return response(status=status.http_404_not_found) serializer = self.get_serializer(instance) # class's `serializer_cass` attribute return response(serializer.data)
as see in stacktrace, error is:
retrieve()
got unexpected keyword argument'pk'
.
look closely in retrieve()
function:
def retrieve(self, request, some_lookup_field=none): # ____________________________^ instance = self.get_object(pk) # handle multi-tenant access control # _________________________^ ...
you taking some_lookup_field
parameter , not pk
, use below in self.get_object(pk)
. try rename some_lookup_field
pk
.
Comments
Post a Comment