diff --git a/rest_framework/generics.py b/rest_framework/generics.py index 6969f1be3..c80f5b202 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -84,29 +84,31 @@ class GenericAPIView(views.APIView): keyword arguments in the url conf. """ queryset = self.filter_queryset(self.get_queryset()) - - # Perform the lookup filtering. - lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field - - assert lookup_url_kwarg in self.kwargs, ( - 'Expected view %s to be called with a URL keyword argument ' - 'named "%s". Fix your URL conf, or set the `.lookup_field` ' - 'attribute on the view correctly.' % - (self.__class__.__name__, lookup_url_kwarg) - ) - lookup_arg = self.lookup_arg - assert lookup_arg in self.request.GET, ( - 'Expected view %s to be called with a URL keyword argument ' - 'named "%s". Fix your URL conf, or set the `.lookup_arg` ' - 'attribute on the view correctly.' % - (self.__class__.__name__, lookup_arg) - ) + if lookup_arg is None: - filter_kwargs = {self.lookup_field: self.request.GET[lookup_arg]} \ - if lookup_arg is not None \ - else {self.lookup_field: self.kwargs[lookup_url_kwarg]} + # Perform the lookup filtering. + lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field + + assert lookup_url_kwarg in self.kwargs, ( + 'Expected view %s to be called with a URL keyword argument ' + 'named "%s". Fix your URL conf, or set the `.lookup_field` ' + 'attribute on the view correctly.' % + (self.__class__.__name__, lookup_url_kwarg) + ) + filter_kwargs = {self.lookup_field: self.kwargs[lookup_url_kwarg]} + + else: + + assert lookup_arg in self.request.GET, ( + 'Expected view %s to be called with a URL keyword argument ' + 'named "%s". Fix your URL conf, or set the `.lookup_arg` ' + 'attribute on the view correctly.' % + (self.__class__.__name__, lookup_arg) + ) + + filter_kwargs = {self.lookup_field: self.request.GET[lookup_arg]} obj = get_object_or_404(queryset, **filter_kwargs)