add checks before make filter kwargs

This commit is contained in:
zzainoo 2022-02-20 03:25:15 +03:00
parent d74a5bfd18
commit c25971c754

View File

@ -84,29 +84,31 @@ class GenericAPIView(views.APIView):
keyword arguments in the url conf. keyword arguments in the url conf.
""" """
queryset = self.filter_queryset(self.get_queryset()) 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 lookup_arg = self.lookup_arg
assert lookup_arg in self.request.GET, ( if lookup_arg is None:
'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]} \ # Perform the lookup filtering.
if lookup_arg is not None \ lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
else {self.lookup_field: self.kwargs[lookup_url_kwarg]}
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) obj = get_object_or_404(queryset, **filter_kwargs)