0

I am having a problem with my DRF API. I want to provide a read only endpoint that takes a single id from a object(document) in the DB and runs a script to perform an check and gives back the result. It works fine when I call the API with //api/documentcheck/1/ where 1 is the pk of the document in the DB. The problem is that if I just call the base URL //api/documentcheck/ it tries to give back the results of all documents in the database and times out because it takes ages.

I am looking for a way to remove the list view and force users to provide the ID for a single document to check.

This is my serializer class

class DocumentCheckSerializer(serializers.Serializer):
    '''Serializer for document check'''
 
    class Meta:
        model = Document
        fields = '__all__'
 
    def to_representation(self, value):
        return process_document(value)

This is my view:

class DocumentCheck(viewsets.ReadOnlyModelViewSet):
    """Check a single document"""
    authentication_classes = (
    TokenAuthentication,
    SessionAuthentication,
    BasicAuthentication,
    )
    permission_classes = [IsAuthenticated]
    queryset = Document.objects.all()
    serializer_class = serializers.DocumentCheckSerializer

and my router entry

router.register("documentcheck", views.DocumentCheck, basename="documentcheck")
1
  • In your viewset, you can override list and return 404 Commented Aug 20, 2022 at 0:42

1 Answer 1

1

You can use just GenericViewSet and RetrieveModelMixin as your base class

from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import RetrieveModelMixin

class DocumentCheck(GenericViewSet, RetrieveModelMixin):
    """Check a single document"""
    authentication_classes = (
    TokenAuthentication,
    SessionAuthentication,
    BasicAuthentication,
    )
    permission_classes = [IsAuthenticated]
    queryset = Document.objects.all()
    serializer_class = serializers.DocumentCheckSerializer
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.