3

Views.py

class CountryViewSet(viewsets.ViewSet):   
    serializer_class = CountrySerializer
    pagination_class = LimitOffsetPagination
    def list(self,request):
        try:
            country_data = Country.objects.all()
            country_serializer = CountrySerializer(country_data,many=True)
            return Response(            
                data = country_serializer.data,
                content_type='application/json',            
                )
        except Exception as ex:
            return Response(
                data={'error': str(ex)},
                content_type='application/json',
                status=status.HTTP_400_BAD_REQUEST
                )

Settings.py i have added

'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',

in my urls.py

router = routers.DefaultRouter(trailing_slash=False)

router.register(r'country', CountryViewSet, base_name='country')
urlpatterns = [
    url(r'^', include(router.urls)),
]

When I try with this URL http://192.168.2.66:8001/v1/voucher/country it is returning all data.

But when I am trying with this URL http://192.168.2.66:8001/v1/voucher/country/?limit=2&offset=2

but it is returning 404 error. I am new to django.kindly help me :)

Zub
  • 808
  • 3
  • 12
  • 23
Susaj S N
  • 960
  • 1
  • 10
  • 22
  • 1
    Surely you get 404 because you've set trailing_slash=False but you are using a trailing slash in your URL. This has nothing to do with offsets. – Daniel Roseman Dec 12 '17 at 09:50

1 Answers1

5

Use ModelViewSet not ViewSet. Also remove your list function it will automatically send response.

from rest_framework.pagination import LimitOffsetPagination

class CountryViewSet(viewsets.ModelViewSet):
    """
    A simple ViewSet for viewing and editing country.
    """ 
    queryset = Country.objects.all()
    serializer_class = CountrySerializer
    pagination_class = LimitOffsetPagination

The actions provided by the ModelViewSet class are .list(), .retrieve(), .create(), .update(), .partial_update(), and .destroy().

UPDATE

In your settings.py

REST_FRAMEWORK = {
    'PAGE_SIZE': 10,
    # 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
}

UPDATE 2

Alternatively, you can just use paginate_queryset and get_paginated_response

def list(self,request):
    country_data = Country.objects.all()

    page = self.paginate_queryset(country_data)
    if page is not None:
       serializer = self.get_serializer(page, many=True)
       return self.get_paginated_response(serializer.data)

    serializer = self.get_serializer(country_data, many=True)
    return Response(serializer.data)

Reference: marking-extra-actions-for-routing

Satendra
  • 6,755
  • 4
  • 26
  • 46
  • if i use ModelViewSet, then can i use list(),retrieve(),create(),update(),destroy() options? – Susaj S N Dec 12 '17 at 09:28
  • Still i am facing the same. :( – Susaj S N Dec 12 '17 at 09:34
  • 1
    you need to set `PAGE_SIZE` in your settings.py – Satendra Dec 12 '17 at 09:37
  • check my update try creating another class of pagination and use that class in viewset. – Satendra Dec 12 '17 at 09:44
  • @SusajSNair, is that worked? also have look on DanielRoseman comment – Satendra Dec 12 '17 at 09:55
  • when i try without your custom class and without trailing slash, it is selecting all data.. – Susaj S N Dec 12 '17 at 10:09
  • How can i get limit, offset from request side. It cannot be staic values. By using your custom class it is not possible i guess. – Susaj S N Dec 12 '17 at 10:11
  • yes, check my update remove custom class and set default `PAGE_SIZE`, also remove trailing slash. – Satendra Dec 12 '17 at 10:27
  • i have tried with URL 'http://192.168.2.66:8001/v1/voucher/country?limit=3&offset=2'. And removed your custom class , Removed 'DEFAULT_PAGINATION_CLASS' But still it is selecting all data :( – Susaj S N Dec 12 '17 at 10:35
  • can i look into your code, if you can share it on git, i am also using it in my project and it is working fine – Satendra Dec 12 '17 at 10:36
  • can you do make `trailing_slash=False` to `True` in urls.py and try fetching it with trailing slash i.e `'https://192.168.2.66:8001/v1/voucher/country/?limit=3&offset=2'` – Satendra Dec 12 '17 at 10:39
  • Anyway its not working properly. it is selecting all data.Kindly note that by requesting this URL it is mapping to list() method where i have 'country_data = Country.objects.filter(chrDocumentStatus = 'N')' this statement. Kindly help me i can't share it via Github :( – Susaj S N Dec 12 '17 at 10:54
  • you can use `self.get_paginated_response(serializer.data)` as make your implementation as it is, read docs http://www.django-rest-framework.org/api-guide/viewsets/#marking-extra-actions-for-routing – Satendra Dec 12 '17 at 11:05
  • 'print(self.get_paginated_response(country_serializer.data))' i am getting ** LimitOffsetPagination object has no attribute count ** error – Susaj S N Dec 12 '17 at 11:57
  • But when i am giving {country_data = self.paginate_queryset(country_data)} it is giving proper result .But iwant the next and previous URL with offset and list value. – Susaj S N Dec 12 '17 at 12:03
  • where is his own serializer to get custom fields – giveJob May 26 '18 at 10:00