Pagination and Filtering

326 views
Skip to first unread message

Thiago Mambretti

unread,
Feb 2, 2017, 10:07:00 AM2/2/17
to Django REST framework
Hi all,

Is it possible to use the default pagination backend along with the following filters?

'django_filters.rest_framework.DjangoFilterBackend',
'rest_framework.filters.SearchFilter',
'rest_framework.filters.OrderingFilter',

I'm using a ModelViewSet and the API only when I have only the pagination or the filters activated.

There's no mention on anything about it on the docs so came here for help.

Thanks,

 



Thiago Mambretti

unread,
Feb 2, 2017, 10:09:21 AM2/2/17
to Django REST framework
Sorry,

The error I'm getting is the following:


Environment:

Request Method: GET

Django Version: 1.10.5
Python Version: 2.7.6
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'rest_framework',
 'crispy_forms',
 'apps.geodata',
 'apps.accounts']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/home/vagrant/.virtualenvs/prospect/local/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/home/vagrant/.virtualenvs/prospect/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/home/vagrant/.virtualenvs/prospect/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/vagrant/.virtualenvs/prospect/local/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)

File "/home/vagrant/.virtualenvs/prospect/local/lib/python2.7/site-packages/rest_framework/viewsets.py" in view
  83.             return self.dispatch(request, *args, **kwargs)

File "/home/vagrant/.virtualenvs/prospect/local/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
  477.             response = self.handle_exception(exc)

File "/home/vagrant/.virtualenvs/prospect/local/lib/python2.7/site-packages/rest_framework/views.py" in handle_exception
  437.             self.raise_uncaught_exception(exc)

File "/home/vagrant/.virtualenvs/prospect/local/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
  474.             response = handler(request, *args, **kwargs)

File "/home/vagrant/.virtualenvs/prospect/local/lib/python2.7/site-packages/rest_framework/mixins.py" in list
  42.         page = self.paginate_queryset(queryset)

File "/home/vagrant/.virtualenvs/prospect/local/lib/python2.7/site-packages/rest_framework/generics.py" in paginate_queryset
  170.         if self.paginator is None:

File "/home/vagrant/.virtualenvs/prospect/local/lib/python2.7/site-packages/rest_framework/generics.py" in paginator
  163.                 self._paginator = self.pagination_class()

Exception Type: TypeError at /api/states/
Exception Value: 'list' object is not callable



Xavier Ordoquy

unread,
Feb 2, 2017, 10:09:57 AM2/2/17
to django-res...@googlegroups.com
Hi,

I can’t think of a reason why they wouldn’t work in particular if the documentation doesn’t says otherwise.
Any specific issue that makes you think they don’t ?

Regards,
Xavier Ordoquy,
Linovia.

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Xavier Ordoquy

unread,
Feb 2, 2017, 10:13:44 AM2/2/17
to django-res...@googlegroups.com
This looks more like you’ve set a list for pagination class for that view.
What does your view looks like ?

Regards,
Xavier Ordoquy,
Linovia.

Thiago Mambretti

unread,
Feb 2, 2017, 10:32:44 AM2/2/17
to django-res...@googlegroups.com
Xavier,

My views are pretty simple!

from __future__ import unicode_literals

import django_filters
from rest_framework import viewsets

from .models import Country, State, City
from .serializers import CountrySerializer, StateSerializer, CitySerializer


class CountryViewSet(viewsets.ModelViewSet):
queryset = Country.objects.all()
serializer_class = CountrySerializer
filter_fields = ['name', 'code']
search_fields = ['name', 'code']
ordering_fields = ['name', 'code']


class StateViewSet(viewsets.ModelViewSet):
queryset = State.objects.all()
serializer_class = StateSerializer
filter_fields = ['name', 'code', 'country']
search_fields = ['name', 'code']
ordering_fields = ['name', 'code', 'country']


class CityViewSet(viewsets.ModelViewSet):
queryset = City.objects.all()
serializer_class = CitySerializer
filter_fields = ['name', 'code', 'state']
search_fields = ['name', 'code']
ordering_fields = ['name', 'code', 'state']

To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Django REST framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-rest-framework/geEBt6SYrkg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-rest-framework+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Thiago Mambretti
"What you do in life, echoes in eternity."
Reply all
Reply to author
Forward
0 new messages