Created
October 8, 2012 09:51
-
-
Save tomchristie/3851739 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MultipleObjectBaseView(MultipleObjectMixin, BaseView): | |
""" | |
Base class for generic views onto a queryset. | |
""" | |
pagination_serializer_class = api_settings.PAGINATION_SERIALIZER | |
paginate_by = api_settings.PAGINATE_BY | |
filter_class = None | |
filter_fields = None | |
def get_filter_class(self): | |
""" | |
Return the django-filters `FilterSet` used to filter the queryset. | |
""" | |
if self.filter_class: | |
return self.filter_class | |
if self.filter_fields: | |
class AutoFilterSet(django_filters.FilterSet): | |
class Meta: | |
model = self.model | |
fields = self.filter_fields | |
return AutoFilterSet | |
return None | |
def filter_queryset(self, queryset): | |
filter_class = self.get_filter_class() | |
if filter_class: | |
return self.filter_class(self.request.GET, queryset=queryset) | |
return queryset | |
def get_filtered_queryset(self): | |
return self.filter_queryset(self.get_queryset()) | |
... # pagination methods as before |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment