def post(self, request, *args, **kwargs): json_data = request.body stream = io.BytesIO(json_data) data = JSONParser().parse(stream) eserializer = EmployeeSerializer(data=data) print(eserializer.is_valid()) eserializer.save() msg = {'msg': 'Resource created successfully'} json_data = JSONRenderer().render(msg) return HttpResponse(json_data, content_type='application/json')
def put(self, request, *args, **kwargs): json_data = request.body stream = io.BytesIO(json_data) data = JSONParser().parse(stream) emp = Employee.objects.get(id=data.get('id')) # eserializer=EmployeeSerializer(emp,data=data) eserializer = EmployeeSerializer(emp, data=data, partial=True) if eserializer.is_valid(): eserializer.save() msg = {'msg': 'Resource updated successfully'} json_data = JSONRenderer().render(msg) return HttpResponse(json_data, content_type='application/json') json_data = JSONRenderer().render(eserializer.errors) return HttpResponse(json_data, content_type='application/json')
def post(self, request, *args, **kwargs): json_data = request.body stream = io.BytesIO(json_data) pdata = JSONParser().parse(stream) eserializer = EmployeeSerializer(data=pdata) if eserializer.is_valid(): eserializer.save() msg = {"Resource Created Successfully"} json_data = JSONRenderer().render(msg) return HttpResponse(json_data, content_type='application/json', status=200) json_data = JSONRenderer().render(eserializer.errors) return HttpResponse(json_data, content_type='application/json', status=200)
def put(self, request, *args, **kwargs): json_data = request.body stream = io.BytesIO(json_data) pdata = JSONParser().parse(stream) id = pdata.get('id', None) emp1 = Employee.objects.get(id=id) emp = EmployeeSerializer(emp1, data=pdata, partial=True) if emp.is_valid(): emp.save() msg = {"Resource Updated Successfully"} json_data = JSONRenderer().render(msg) return HttpResponse(json_data, content_type='application/json', status=200) json_data = JSONRenderer().render(emp.errors) return HttpResponse(json_data, content_type='application/json', status=400)