def put(self, request, *args, **kwargs): json_data = request.body #Converting Json data into Python Dictionary stream = io.BytesIO(json_data) python_data = JSONParser().parse(stream) id = python_data.get('id') emp = Employee.objects.get(id=id) # perform Deserialization #convert Python Dictionary into Database code employee_serializer = EmployeeSerializers(emp, data=python_data, partial=True) if employee_serializer.is_valid(): employee_serializer.save() msg = {'msg': 'Resource Updated successfully'} json_data = JSONRenderer().render(msg) return HttpResponse(json_data, content_type='application/json') json_data = JSONRenderer().render(employee_serializer.errors) return HttpResponse(json_data, content_type='application/json', status=400)
def post(self, request, *args, **kwargs): json_data = request.body stream = io.BytesIO(json_data) pdata = JSONParser().parse(stream) serializer = EmployeeSerializers(data=pdata) if serializer.is_valid(): serializer.save() msg = {'msg': 'Record created successfully'} json_data = JSONRenderer().render(msg) return HttpResponse(json_data, content_type='application/json') json_data = JSONRenderer().render(serializer.errors) return HttpResponse(json_data, content_type='application/json')
def put(self, request, *args, **kwargs): json_data = request.body stream = io.BytesIO(json_data) pdata = JSONParser().parse(stream) id = pdata.get('id') emp = Employee.objects.get(id=id) serializer = EmployeeSerializers(emp, data=pdata, partial=True) if serializer.is_valid(): serializer.save() msg = {'msg': 'Record Updated successfully'} json_data = JSONRenderer().render(msg) return HttpResponse(json_data, content_type='application/json') json_data = JSONRenderer().render(serializer.errors) return HttpResponse(json_data, content_type='application/json')
def get(self, request, *args, **kwargs): json_data = request.body stream = io.BytesIO(json_data) pdata = JSONParser().parse(stream) id = pdata.get('id', None) if id is not None: emp = Employee.objects.get(id=id) serializer = EmployeeSerializers(emp) json_data = JSONRenderer().render(serializer.data) return HttpResponse(json_data, content_type='application/json') qs = Employee.objects.all() serializer = EmployeeSerializers(qs, many=True) json_data = JSONRenderer().render(serializer.data) return HttpResponse(json_data, content_type='application/json')
def post(self,request,*args,**kwargs): json_data = request.body #Converting Json data into Python Dictionary stream = io.BytesIO(json_data) python_data = JSONParser().parse(stream) #converting Python Dictionary into database code employee_serializer = EmployeeSerializers(data = python_data) if employee_serializer.is_valid(): employee_serializer.save() msg = {'msg':'Resource created successfully'} json_data = JSONRenderer().render(data = msg) return HttpResponse(json_data,content_type='application/json') json_data = JSONRenderer().render(employee_serializer.errors) return HttpResponse(json_data,content_type='application/json',status=400)
def get(self,request,*args,**kwargs): json_data = request.body #Converting Json data into Python Dictionary stream = io.BytesIO(json_data) python_data = JSONParser().parse(stream) id = python_data.get('id',None) if id is not None: emp = Employee.objects.get(id=id) #converting Python Dictionary into database code employee_serializer = EmployeeSerializers(emp) json_data = JSONRenderer().render(data = employee_serializer.data) return HttpResponse(json_data,content_type='application/json') query_string = Employee.objects.all() employee_serializer = EmployeeSerializers(query_string,many=True) json_data = JSONRenderer().render(data = employee_serializer.data) return HttpResponse(json_data,content_type='application/json')