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 get(self, request, *args, **kwargs): json_data = request.body stream = io.BytesIO(json_data) data = JSONParser().parse(stream) id = data.get('id', None) if id is not None: emp = Employee.objects.get(id=id) serializer = EmployeeSerializer(emp) json_data = JSONRenderer().render(serializer.data) return HttpResponse(json_data, content_type='application/json') qs = Employee.objects.all() serializer = EmployeeSerializer(qs, many=True) json_data = JSONRenderer().render(serializer.data) 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)
def get(self, request, *args, **kwargs): json_data = request.body stream = io.BytesIO(json_data) pdata = JSONParser().parse(stream) print(pdata) id_exits = pdata.get('id', None) if id_exits != None: emp = Employee.objects.get(id=id_exits) eserializer = EmployeeSerializer(emp) json_data = JSONRenderer().render(eserializer.data) return HttpResponse(json_data, content_type='application/json', status=200) qs = Employee.objects.all() eserializer = EmployeeSerializer(qs, many=True) json_data = JSONRenderer().render(eserializer.data) return HttpResponse(json_data, content_type='application/json', status=200)
def get(self, request, format=None): qs = Employee.objects.all() serializer = EmployeeSerializer(qs, many=True) return Response(serializer.data)