Django rest framework pagination
<p>I am trying to add pagination into my project, couldn't find any clear documentation or tutorial.</p>

<p>I have a list of offices</p>

<p>models
Office.py</p>

<pre><code>class Office(Model):
    name = CharField(_("name"), default=None, max_length=255, null=True)
    email = EmailField(_("email"), default=None, max_length=255, null=True)
    description = TextField(_("description"), default=None, null=True)
</code></pre>

<p>Serializer</p>

<pre><code>class OfficeSerializer(ModelSerializer):
     id = IntegerField(read_only=True)
     name = CharField(read_only=True)
     email = URLField(read_only=True)
     description = CharField(read_only=True)

class Meta:
    model = Office
    fields = ("id", "name", "email", "description")
</code></pre>

<p>views.py</p>

<pre><code>@api_view(["GET"])
@permission_classes((AllowAny,))
def offices(request):
instance = Office.objects.filter()[:10]
serializer = OfficeSerializer(instance, many=True)

return Response(serializer.data)
</code></pre>

<p>Any help with returning Office list with pagination ?</p>

<p><a href="http://www.django-rest-framework.org/api-guide/pagination/" rel="nofollow">http://www.django-rest-framework.org/api-guide/pagination/</a></p>

<blockquote>
  <p>Pagination is only performed automatically if you're using the generic
  views or viewsets. If you're using a regular APIView, you'll need to
  call into the pagination API yourself to ensure you return a paginated
  response. See the source code for the mixins.ListModelMixin and
  generics.GenericAPIView classes for an example.</p>
</blockquote>

<p><a href="https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/mixins.py#L35" rel="nofollow">https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/mixins.py#L35</a>
<a href="https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/generics.py#L166" rel="nofollow">https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/generics.py#L166</a></p>

<p>so I would suggest something like:</p>

<pre><code>@api_view(["GET"])
@permission_classes((AllowAny,))
def offices(request):
    pagination_class = api_settings.DEFAULT_PAGINATION_CLASS
    paginator = pagination_class()
    queryset = Office.objects.all()
    page = paginator.paginate_queryset(queryset, request)

    serializer = OfficeSerializer(page, many=True)

    return paginator.get_paginated_response(serializer.data)
</code></pre>

python django pagination django-rest-framework