-> 이러한 기능들을 APIView class를 통해 간단하게 구현 가능
class PatientDetailAPIView(APIView):
def get_object(self, pk):
try:
return Patient.objects.get(pk=pk)
except Patient.DoesNotExist:
raise Http404
def get(self, request, pk):
patient = self.get_object(pk)
serializer = PatientSerializer(patient)
return Response(serializer.data)
def put(self, request, pk):
patient = self.get_object(pk)
serializer = PatientSerializer(patient)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
DRF의 generics
viewset
# ==== views.py ====
class PatientViewSet(ModelViewSet):
queryset = Patient.objects.all()
serializer_class = PatientSerializer