Commit 82e9ddcf by Yamila Moreno

Added get_filter_backends method

parent 4d894fd3
...@@ -121,6 +121,22 @@ For example: ...@@ -121,6 +121,22 @@ For example:
Note that if your API doesn't include any object level permissions, you may optionally exclude the ``self.check_object_permissions, and simply return the object from the `get_object_or_404` lookup. Note that if your API doesn't include any object level permissions, you may optionally exclude the ``self.check_object_permissions, and simply return the object from the `get_object_or_404` lookup.
#### `get_filter_backends(self)`
Returns the classes that should be used to filter the queryset. Defaults to returning the `filter_backends` attribute.
May be override to provide more complex behavior with filters, as using different (or even exlusive) lists of filter_backends depending on different criteria.
For example:
def get_filter_backends(self):
if "geo_route" in self.request.QUERY_PARAMS:
return (GeoRouteFilter, CategoryFilter)
elif "geo_point" in self.request.QUERY_PARAMS:
return (GeoPointFilter, CategoryFilter)
return (CategoryFilter,)
#### `get_serializer_class(self)` #### `get_serializer_class(self)`
Returns the class that should be used for the serializer. Defaults to returning the `serializer_class` attribute, or dynamically generating a serializer class if the `model` shortcut is being used. Returns the class that should be used for the serializer. Defaults to returning the `serializer_class` attribute, or dynamically generating a serializer class if the `model` shortcut is being used.
...@@ -328,7 +344,7 @@ You can then simply apply this mixin to a view or viewset anytime you need to ap ...@@ -328,7 +344,7 @@ You can then simply apply this mixin to a view or viewset anytime you need to ap
serializer_class = UserSerializer serializer_class = UserSerializer
lookup_fields = ('account', 'username') lookup_fields = ('account', 'username')
Using custom mixins is a good option if you have custom behavior that needs to be used Using custom mixins is a good option if you have custom behavior that needs to be used
## Creating custom base classes ## Creating custom base classes
...@@ -337,7 +353,7 @@ If you are using a mixin across multiple views, you can take this a step further ...@@ -337,7 +353,7 @@ If you are using a mixin across multiple views, you can take this a step further
class BaseRetrieveView(MultipleFieldLookupMixin, class BaseRetrieveView(MultipleFieldLookupMixin,
generics.RetrieveAPIView): generics.RetrieveAPIView):
pass pass
class BaseRetrieveUpdateDestroyView(MultipleFieldLookupMixin, class BaseRetrieveUpdateDestroyView(MultipleFieldLookupMixin,
generics.RetrieveUpdateDestroyAPIView): generics.RetrieveUpdateDestroyAPIView):
pass pass
......
...@@ -175,6 +175,14 @@ class GenericAPIView(views.APIView): ...@@ -175,6 +175,14 @@ class GenericAPIView(views.APIView):
method if you want to apply the configured filtering backend to the method if you want to apply the configured filtering backend to the
default queryset. default queryset.
""" """
for backend in self.get_filter_backends():
queryset = backend().filter_queryset(self.request, queryset, self)
return queryset
def get_filter_backends(self):
"""
Returns the list of filter backends that this view requires.
"""
filter_backends = self.filter_backends or [] filter_backends = self.filter_backends or []
if not filter_backends and self.filter_backend: if not filter_backends and self.filter_backend:
warnings.warn( warnings.warn(
...@@ -185,10 +193,8 @@ class GenericAPIView(views.APIView): ...@@ -185,10 +193,8 @@ class GenericAPIView(views.APIView):
PendingDeprecationWarning, stacklevel=2 PendingDeprecationWarning, stacklevel=2
) )
filter_backends = [self.filter_backend] filter_backends = [self.filter_backend]
return filter_backends
for backend in filter_backends:
queryset = backend().filter_queryset(self.request, queryset, self)
return queryset
######################## ########################
### The following methods provide default implementations ### The following methods provide default implementations
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment