How to draw with filters in django rest framework

I currently have an API view setup as follows:

class WeatherObservationSerializer(serializers.ModelSerializer): dew_point = serializers.Field(source='dew_point') wind_gust = serializers.Field(source='get_wind_gust') class Meta: model = WeatherObservation fields = ('id', 'station', 'temperature', 'pressure', 'humidity', 'wind_direction', 'wind_speed', 'rainfall', 'date', 'dew_point', 'wind_gust') class WeatherObservationList(generics.ListCreateAPIView): model = WeatherObservation serializer_class = WeatherObservationSerializer permission_classes = (permissions.IsAuthenticatedOrReadOnly,) def get_queryset(self): queryset = WeatherObservation.objects.all() min_date = self.request.QUERY_PARAMS.get('min_date', None) station = self.request.QUERY_PARAMS.get('station', None) if min_date is not None: queryset = queryset.filter(date__gte=min_date) if station is not None: queryset = queryset.filter(station=station) return queryset 

My settings.py contains: REST_FRAMEWORK = {'PAGINATE_BY': 50, 'PAGINATE_BY_PARAM': 'page'}

When I make an API request, for example: / api / weather / observations /? station = 2 & page = 2 & min_date = 2013-3-14 I return only two results. If it is for pages 3, 3 of the result, etc. Is there something that I am doing wrong that is causing this problem?

Greetings.

+8
source share
2 answers

Mark the docs for these settings:

PAGINATE_BY_PARAM

The name of the request parameter that the client can use to override the default page size used for pagination. If set to No, clients cannot override the default page size.

Just remove this line from settings.py settings and everything will be fine.

UPDATE 1/7/2016:

Note that this option is now in the deprecation process. See the page guide for more information.

The short option is that now you have to create your own Pagination class with the appropriate settings, which then apply to your view. The examples in the linked manual should be more than helpful.

+4
source

Not sure how useful this would be, but I needed to paginate one view in my project that uses a filter. what i did was

 class GlobalFilter(CustomMixin): from rest_framework.pagination import PageNumberPagination queryset = Data.objects.all() filter_backends = [DjangoFilterBackend] serializer_class = FilterSerializer filterset_class = GlobalFilterSet pagination_class = PageNumberPagination pagination_class.page_size = 100 

May be useful for someone else, maybe :)

0
source

All Articles