[Answered ]-Optimize getting the first record in a table using an API endpoint with Django REST Framework and/or Django-filter

1👍

The error comes from this line :

first_id = first_item[0]

This is because first_item is already a RecordReview, as it was retrieved using first()

You can simplify get_queryset as follows though :

class ReviewViewSet(ModelViewSet):
    filter_backends = (DjangoFilterBackend, OrderingFilter)
    serializer_class = ReviewSerializer
    permission_classes = [permissions.IsAuthenticated]

    def get_queryset(self):
        first = self.request.query_params.get('first')
        queryset = RecordReview.objects.all()

        if first is not None:
            queryset = queryset.order_by('id')[:1]

        return queryset

Leave a comment