ExamHoot

Pagination and Filtering in DRF Scenario

from rest_framework import generics, filters
from rest_framework.pagination import PageNumberPagination
from django_filters.rest_framework import DjangoFilterBackend

class StandardPagination(PageNumberPagination):
    page_size = 10
    page_size_query_param = 'page_size'
    max_page_size = 100

class StudentListView(generics.ListAPIView):
    queryset = Student.objects.all()
    serializer_class = StudentSerializer
    pagination_class = StandardPagination
    filter_backends = [DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter]
    filterset_fields = ['department', 'year']
    search_fields = ['name', 'email']
    ordering_fields = ['name', 'created_at']
    ordering = ['-created_at']
  1. A request to `/students/?department=CSE&search=Hari&ordering=name&page=2&page_size=20` will filter by department, search by name containing 'Hari', order by name, and return page 2 with 20 results per page.

  2. The max_page_size=100 means users cannot request more than 100 results, but the default page_size=10 will always be used regardless of page_size parameter.

  3. DjangoFilterBackend and SearchFilter cannot be used together; you must choose one filtering method.

  4. The ordering='-created_at' sets a hard default that cannot be overridden by the ordering query parameter.

Show answer & explanation

Correct answer

A request to `/students/?department=CSE&search=Hari&ordering=name&page=2&page_size=20` will filter by department, search by name containing 'Hari', order by name, and return page 2 with 20 results per page.

Explanation

DRF supports multiple filter backends that work together: DjangoFilterBackend for exact/range filters, SearchFilter for text search, OrderingFilter for sorting. Pagination divides results into pages.

Written by ExamHoot EditorialPublished · Updated

All 29 Django REST API Development questions

  1. 1.What is the primary purpose of Django REST Framework (DRF)?
  2. 2.What is a Serializer in Django REST Framework?
  3. 3.Which decorator is used to convert a regular Django view into an API view in DRF?
  4. 4.What is the purpose of ViewSets in Django REST Framework?
  5. 5.Which class should be inherited to create a basic API view in DRF?
  6. 6.What is the default authentication method in Django REST Framework?
  7. 7.How do you register URLs for ViewSets in Django REST Framework?
  8. 8.What does the status module in DRF provide?
  9. 9.Which permission class allows only authenticated users to access an API endpoint?
  10. 10.What is the Response class used for in Django REST Framework?
  11. 11.Scenario:
  12. 12.What will be the output of the following code snippet?
  13. 13.Scenario:
  14. 14.What is the difference between ModelSerializer and Serializer in Django REST Framework?
  15. 15.Identify the bug in the following code snippet:
  16. 16.How do you implement pagination in a Django REST Framework API?
  17. 17.Scenario:
  18. 18.What is the purpose of the queryset attribute in a ViewSet?
  19. 19.Scenario:
  20. 20.What will be the output of this code snippet?
  21. 21.Understanding DRF Serializers and Nested Relationships
  22. 22.DRF Viewsets and Router Configuration Scenario
  23. 23.Authentication and Permissions in DRF
  24. 24.Pagination and Filtering in DRF Scenario
  25. 25.Custom Validation in DRF Serializers
  26. 26.Status Codes and Exception Handling in DRF
  27. 27.Throttling and Rate Limiting Scenario
  28. 28.Request and Response Cycle with Middleware
  29. 29.Advanced Serializer Optimization and Performance