From c25971c754a4bb3cd9ff07008d84a745fb72a2c2 Mon Sep 17 00:00:00 2001 From: zzainoo Date: Sun, 20 Feb 2022 03:25:15 +0300 Subject: [PATCH] add checks before make filter kwargs --- rest_framework/generics.py | 42 ++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 20 deletions(-) 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)