0% found this document useful (0 votes)
9 views

Exp 9 Employee Management System using Django DRF (1)

The document outlines a lab manual for an Employee Management System using Django REST Framework (DRF), focusing on CRUD operations. It includes prerequisites, setup instructions, and code examples for creating a REST API to manage employee records. Additionally, it provides testing guidelines using tools like Postman or Thunder Client and requires students to submit their code and observations after completing the experiment.

Uploaded by

Hemil Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Exp 9 Employee Management System using Django DRF (1)

The document outlines a lab manual for an Employee Management System using Django REST Framework (DRF), focusing on CRUD operations. It includes prerequisites, setup instructions, and code examples for creating a REST API to manage employee records. Additionally, it provides testing guidelines using tools like Postman or Thunder Client and requires students to submit their code and observations after completing the experiment.

Uploaded by

Hemil Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

SVKM’s NMIMS

Mukesh Patel School of Technology Management & Engineering (Mumbai Campus)


IMAD
Lab Manual
PART A
Experiment No. 09
A.1 Aim: Employee Management System using Django REST Framework (DRF )

Objective: To learn and understand how use CRUD operations in DRF

A.2 Prerequisite: HTML, CSS, Javascript, CURD in Django, Postman or Thunder Client

A.3 Outcome:
After successful completion of these applications students will be able to understand Django
DRF Framework

A.4 Theory: CRUD Operations in Django using DRF


CRUD stands for Create, Read, Update, and Delete, which are the four basic operations used to
manage data in a database cane be handled by the function in views.py using DRF

Steps for Setting up Django REST Framework (DRF)

1. Install DRF: pip install djangorestframework

2. Configure Django Settings:

1. Add 'rest_framework' to your INSTALLED_APPS list in settings.py:

INSTALLED_APPS = [
...,
'rest_framework',
]
3. Create Serializer for Already created Employee Model: In myapp/serializers.py:

from rest_framework import serializers


from .models import Employee

class EmployeeSerializer(serializers.ModelSerializer):
class Meta:
model = Employee
fields = ['id', 'name', 'salary']
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering (Mumbai Campus)
IMAD
Lab Manual

4. Update Views with API Endpoints: In myapp/views.py:

from rest_framework.decorators import api_view


from rest_framework.response import Response
from rest_framework import status
from .models import Employee
from .serializers import EmployeeSerializer

# List Employees or Create new Employee


@api_view(['GET', 'POST'])
def employee_list(request):
if request.method == 'GET':
employees = Employee.objects.all()
serializer = EmployeeSerializer(employees, many=True)
return Response(serializer.data)…..
….

Scenario Problem statement 1: Employee Management System Using Django DRF

"Design and implement a Django-based REST API for managing employee records. This API should
allow clients to perform CRUD operations on employee data.

Hints:

myproject/ # Root directory of your Django project



├── myproject/ # Django settings and configuration files
│ ├── __init__.py
│ ├── settings.py # Project settings
│ ├── urls.py # Root URL configuration
│ ├── asgi.py
│ └── wsgi.py

├── myapp3/ # Application directory (the app for employee management)
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py # Application configuration
│ ├── forms.py # Django forms for Employee model
│ ├── migrations/ # Database migrations for the app
│ │ └── __init__.py
│ ├── models.py # Employee model definition
│ ├── views.py # Application views (CRUD and employee-related operations)
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering (Mumbai Campus)
IMAD
Lab Manual
│ ├── urls.py # URL routing for the app (employee CRUD operations)
│ └── templates/
│ ├── employee_form.html # Template for employee creation and update
│ ├── employee_list.html # Template to display employee list
│ └── employee_confirm_delete.html # Template for employee deletion confirmation

├── manage.py
└── db.sqlite3

Sampe 2. URL Configuration (myapp/urls.py)

from django.urls import path


from . import views

urlpatterns = [
path('api/employees/', views.employee_list, name='employee_list_api'), # List or Create
path('api/employees/<int:id>/', views.employee_detail, name='employee_detail_api'), # Get, Update or Delete
]

Note: Just by using the 2 uri you will be able to perform the basic crud operations

Problem statement 2: Testing with Thunder Client or Postman


Test the API using Thunder Client or Postman for verifying GET, POST, PUT, and DELETE requests.
Create a new employee:
Method: POST
URL: http://127.0.0.1:8000/myapp/api/employees/
Request Body:
{
"name": "Tom",
"salary": 50000.00
}
List all employees:
Method: GET
URL: http://127.0.0.1:8000/myapp/api/employees/
Retrieve an employee by ID:
Method: GET
URL: http://127.0.0.1:8000/myapp/api/employees/1/
Update an employee by ID:
Method: PUT
URL: http://127.0.0.1:8000/myapp/api/employees/1/
Request Body:
{
"name": "Jerry",
"salary": 60000.00
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering (Mumbai Campus)
IMAD
Lab Manual
}
Delete an employee by ID:
Method: DELETE
URL: http://127.0.0.1:8000/myapp/api/employees/1/

Directory Structure of the Complete Django App


myproject/

├── manage.py
├── myproject/ # Project settings directory
│ ├── __init__.py
│ ├── settings.py # Django settings
│ ├── urls.py # Project-level URL routing
│ └── wsgi.py

├── myapp/ # Your Django app (e.g., Employee management)
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── models.py # Define Employee model here
│ ├── serializers.py # Define Employee serializer here
│ ├── urls.py # App-level URL routing
│ └── views.py # Define API views for CRUD operations
└── db.sqlite3 # SQLite database file
ART B

(PART B: TO BE COMPLETED BY STUDENTS)

(Students must submit the soft copy as per following segments within two hours of the practical. The
soft copy must be uploaded on the Blackboard or emailed to the concerned lab in charge faculties
at the end of the practical in case the there is no Black board access available)

Roll No. : Name:


Class : Batch :
Date of Experiment : Date/Time of Submission :
Grade :

B.1 Code:

(Paste your Code here)

B.2 Output

(Take screen shots of the output at run time and paste it here)
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering (Mumbai Campus)
IMAD
Lab Manual
B.3 Conclusion:

(Students must write the conclusion as per the attainment of individual outcome listed above)

B.3 Observations and Learning:

(Students must write their observations and learnings as per the attainment of individual outcome listed
above)

B.4 Question of Curiosity

(To be answered by student based on the practical performed and learning/observations)

1. How does Django REST Framework (DRF) handle serialization for database models?
2. What is the role of api_view in Django REST Framework?
3. How do you implement authentication and permissions in Django REST Framework?
4. What are the advantages of using class-based views over function-based views in DRF?
5. What is the difference between Serializer and ModelSerializer in Django REST Framework?
6. How does Django REST Framework handle versioning of APIs, and why is it important?

You might also like