I'm having an issue with paginating json data that is sorted by the rest_framework 's SerializerMethodField . Before I began to add pagination to my list views, I had the sorted json data in a context variable like this:

class ExampleList(ListView): ... def get_context_data(self, **kwargs): context = super(ExampleList, self).get_context_data(**kwargs) context["examples"] = sorted(ExampleSerializer( submissions, many=True, context={'request': self.request} ).data, key=lambda x: x.get("score"), reverse=True) return context ...

This worked perfectly, because the lambda function grabbed the score, and ordered by it, exactly how sorted() should work. The problem began with pagination. I've researched for a few days now, and there aren't any methods to paginate by json data that I can find. Only by querysets.

When I started paginating, here are my two Serializer Classes:

class ExampleSerializer(serializers.ModelSerializer): score = serializers.SerializerMethodField('get_score') class Meta: model = Example fields = ('id', '...', 'score',) def get_score(self, obj): return obj.calculate_score() class PaginatedExampleSerializer(pagination.PaginationSerializer): class Meta: object_serializer_class = ExampleSerializer

In one of my list views, I've created a sorted context object that sorts the serialized data by score and paginates it. I, also, created a method to call for pagination called paginate_examples() . As you can see, it paginates by the queryset first, and then sorts the data by score on each paginated page. So something that should be on page 1 is all the way back on page 5 or so.

class ExampleList(ListView): queryset = Example.objects.all() def paginate_examples(self, queryset, paginate_by): paginator = Paginator(queryset, paginate_by) page = self.request.GET.get('page') try: examples = paginator.page(page) except PageNotAnInteger: examples = paginator.page(1) except EmptyPage: examples = paginator.page(paginator.num_pages) return PaginatedExampleSerializer(examples, context={'request': self.request}).data def get_context_data(self, **kwargs): context = super(ExampleList, self).get_context_data(**kwargs) pagination = self.paginate_examples(self.queryset, self.paginate_by) examples = pagination.get("results") context["examples"] = sorted(examples, key=lambda x: x.get("score"), reverse=True) context["pagination"] = pagination return context

Again, the problem is that list items that should be displaying on /?page=1 are displaying on /?page=x because the PaginatedExampleSerializer paginates the data before it gets sorted by the SerializerMethodField .