<file_sep>from .models import Employee
from rest_framework import serializers



class EmployeeSerializer(serializers.ModelSerializer):
	class Meta:
		model=Employee
		fields=('Employee_Id','Employee_Name','Department_Name')<file_sep># -*- coding: utf-8 -*-
# Generated by Django 1.11.7 on 2017-11-24 19:15
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Employee',
            fields=[
                ('Employee_Id', models.IntegerField(primary_key=True, serialize=False)),
                ('Employee_Name', models.CharField(max_length=50)),
                ('Department_Name', models.CharField(max_length=50)),
            ],
        ),
    ]
<file_sep># DJango-RESTframework
Hi, This is all about REST Services for CRUD operations. I am newbie to DJangoRestFramework, hope this helps guys who wanted to know some basic idea about APIs and how it works.


Created Individual apis for each individual operations like (get all, get by Id, Inserting record, updating record by Id, deleting record by Id.
at begining struggled bit,anyway  everthing working fine. 

Tools used:
Postman : to test apis
Database: MySql



HappyLearning !!
<file_sep>from __future__ import unicode_literals
from django.db import models

# Create your models here.


class Employee(models.Model):
	Employee_Id=models.IntegerField(primary_key = True)
	Employee_Name=models.CharField(max_length=50)
	Department_Name=models.CharField(max_length=50)


<file_sep>from django.http import Http404
from rest_framework.views import APIView
from rest_framework.response import Response
from django.core.serializers import serialize
from .models import Employee
from .serializers import EmployeeSerializer
from rest_framework import status
import pdb

# create your views here

# Get Record By ID(primary key)
class EmployeeDetailsByID(APIView):
	
	def get_object(self,pk):
		try:
			return Employee.objects.get(pk=pk)
		except Employee.DoesNotExist:
			raise Http404

	def get(self,request,pk,format=None):
		employee=self.get_object(pk)
		serializer=EmployeeSerializer(employee)
		return Response(serializer.data)

# Get all records present in requested database

class EmployeesDetails(APIView):

	def get(self,request,format=None):
		employee=Employee.objects.all()
		serializer=EmployeeSerializer(employee,many=True)
		return Response(serializer.data)

# Inserting new record into the database

class InsertEmployee(APIView):
	def post(self,request,format=None):
		try:
			
			serializer=EmployeeSerializer(data=request.data)
			
			if serializer.is_valid():
				serializer.save()
				return Response(serializer.data,status=status.HTTP_201_CREATED)
		except Employee.DoesNotExist:
			raise Http404

# Updating record based on ID (key)

class UpdateEmployee(APIView):

	def put(self,request,pk,format=None):
		try:
			employee=EmployeeDetailsByID.get_object(self,pk)
			
			data=(EmployeeSerializer(employee).data)
			requested_data=request.data
			data.update(requested_data)
			serializer=EmployeeSerializer(employee,data=data)
			
			if serializer.is_valid():
				serializer.save()
				return Response(data,status=status.HTTP_201_CREATED)

			
		except Employee.DoesNotExist:
			raise Http404
# Deleting  record based on ID(key)

class DeleteEmployee(APIView):

	def delete(self,request,pk,format=None):

		try:
			employee=EmployeeDetailsByID.get_object(self,pk)
			employee.delete()
			return Response(status=status.HTTP_204_NO_CONTENT)
			

		except Employee.DoesNotExist:
			raise Http404



<file_sep>from django.conf.urls import url
from . import views


urlpatterns =[


		url(r'^api/employees/$',views.EmployeesDetails.as_view()),

		url(r'^api/employees/(?P<pk>[0-9]+)/$',views.EmployeeDetailsByID.as_view()),

		url(r'^api/insertemployee/$',views.InsertEmployee.as_view()),

		url(r'^api/updateemployee/(?P<pk>[0-9]+)/$', views.UpdateEmployee.as_view()),
		
		url(r'^api/deleteemployee/(?P<pk>[0-9]+)/$', views.DeleteEmployee.as_view()),

]
