def device_listAPI(request, format=None): if request.method == "GET": devices = Device.objects.all() serializer = DevicesSerializer(devices, many=True) return Response(serializer.data) elif request.method == "POST": serializer = DevicesSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def device_detailAPI(request, pk, format=None): try: device = Device.objects.get(pk=pk) except Device.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = DevicesSerializer(device) return Response(serializer.data) elif request.method == 'PUT': serializer = DevicesSerializer(device, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.erors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': device.delete() return Response(status=status.HTTP_204_NO_CONTENT)