Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
Create custom abstract view class inherited from APIView

I am working in Django with Django REST Framework module.

For each model I make, I have a view:

class CustomAPIView(APIView):
    renderer_classes = (JSONRenderer, )
    permission_classes = (IsAuthenticated, )

    @csrf_exempt
    def post(self, request):
        raw_data = serializers.SearchStateSerializer(data=request.data)
        if raw_data.is_valid():
            searched_data = serializers.ShowStateSerializer(data=serializers.State.objects.extra(where=raw_data.data['where'], order_by=raw_data.data['order_by']), many=True)
            return JsonResponse(paginate_data(searched_data=searched_data, request_data=raw_data), status=status.HTTP_202_ACCEPTED)

        else:
            return JsonResponse(raw_data.errors, status=status.HTTP_400_BAD_REQUEST)

In this code, there are 3 constraints that change:

  1. SearchStateSerializer
  2. ShowStateSerializer
  3. State(Model)

So I want to create an abstract in which I only specify these 3 things and the view works. How can I do this? I searched a lot but with no luck. It is must that these 3 constraints are supplied or it will throw error.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

use this as your parent class:

class CustomAPIView(APIView):
    renderer_classes = (JSONRenderer, )
    permission_classes = (IsAuthenticated, )
    search_state_serializer_class = None
    show_state_serializer_class = None
    state_model = None


    @csrf_exempt
    def post(self, request):
        raw_data = self.search_state_serializer_class(data=request.data)
        if raw_data.is_valid():
            searched_data = self.show_state_serializer_class(
                                data=(self.state_model.objects
                                      .extra(where=raw_data.data['where'],
                                             order_by=raw_data.data['order_by'])),
                                      many=True)
        return JsonResponse(paginate_data(searched_data=searched_data, request_data=raw_data), status=status.HTTP_202_ACCEPTED)
    else:
        return JsonResponse(raw_data.errors, status=status.HTTP_400_BAD_REQUEST)

now you can simply provide values for class fields and the post method will work.

class MyView(CustomApiView):
    search_state_serializer_class = MySearchStateSerializer
    show_state_serializer_class = MyShowStateSerializer
    state_model = MyStateModel
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!