0

I want to write a class based API view to check if the record exists in DB or not then return True else False by using rest_framework. How could I create CBV to check it? Please help me with this context. here is my serializer class

class EmployeeSerializer(ModelSerializer):
class Meta:
    model = Employee
    fields = '__all__'

here is my url

path('employee/<name>/<code>/',views.EmployeeExist.as_view(),name = 'employee_exits')

1 Answer 1

1

Here is how you can create simple view:

from rest_framework import status, response
from rest_framework import generics

class EmployeeExistView(generics.GenericAPIView):
   serializer_class = None

   def get(self, request, *args, **kwargs):
       employee = Employee.objects.filter(id=kwargs.get('id'))
       if employee.exists():
           return response.Response(status=status.HTTP_200_OK)
       return response.Response(status=status.HTTP_404_NOT_FOUND)
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.