class ListModelMixin

from rest_framework.mixins import ListModelMixin
Documentation Source code
    List a queryset.
    Should be mixed in with `MultipleObjectAPIView`.
    

Ancestors (MRO)

  1. ListModelMixin

Attributes

  Defined in
empty_error = Empty list and '%(class_name)s.allow_empty' is False. ListModelMixin

Methods used but not implemented in this class

  • get_pagination_serializer
  • get_serializer
  • filter_queryset
  • paginate_queryset
  • get_queryset
  • get_paginate_by
  • get_allow_empty
Expand Collapse

Methods

def list(self, request, *args, **kwargs): ΒΆ ListModelMixin

    def list(self, request, *args, **kwargs):
        queryset = self.get_queryset()
        self.object_list = self.filter_queryset(queryset)

        # Default is to allow empty querysets.  This can be altered by setting
        # `.allow_empty = False`, to raise 404 errors on empty querysets.
        allow_empty = self.get_allow_empty()
        if not allow_empty and not self.object_list:
            class_name = self.__class__.__name__
            error_msg = self.empty_error % {'class_name': class_name}
            raise Http404(error_msg)

        # Pagination size is set by the `.paginate_by` attribute,
        # which may be `None` to disable pagination.
        page_size = self.get_paginate_by(self.object_list)
        if page_size:
            packed = self.paginate_queryset(self.object_list, page_size)
            paginator, page, queryset, is_paginated = packed
            serializer = self.get_pagination_serializer(page)
        else:
            serializer = self.get_serializer(self.object_list)

        return Response(serializer.data)