Make a django drf employee management mini project with name,slary,location ,mobile no
- User
Make a django drf employee management mini project with name,slary,location ,mobile no
- Assistant
Make a django drf employee management mini project with name,slary,location ,mobile no
Sure! Below is a step-by-step guide for creating a Django REST Framework mini-project called Employee Management with the following fields:
namesalarylocationmobile_noWe’ll cover:
First, make sure you have Django and DRF installed:
pip install django djangorestframework
Now create the Django project and app:
django-admin startproject employee_project
cd employee_project
python manage.py startapp employee
Add 'rest_framework' and 'employee' to your INSTALLED_APPS in employee_project/settings.py:
INSTALLED_APPS = [
...
'rest_framework',
'employee',
]
employee_project/
│
├── employee_project/
│ └── settings.py
│
├── employee/
│ └── models.py
│ └── views.py
│ └── serializers.py # ← will create
│ └── urls.py # ← will create
│
└── manage.py
Edit employee/models.py:
from django.db import models
class Employee(models.Model):
name = models.CharField(max_length=100)
salary = models.DecimalField(max_digits=10, decimal_places=2)
location = models.CharField(max_length=100)
mobile_no = models.CharField(max_length=15)
def __str__(self):
return self.name
Run migrations:
python manage.py makemigrations
python manage.py migrate
Create a file employee/serializers.py:
from rest_framework import serializers
from .models import Employee
class EmployeeSerializer(serializers.ModelSerializer):
class Meta:
model = Employee
fields = '__all__'
Edit employee/views.py to use ViewSets:
from rest_framework import viewsets
from .models import Employee
from .serializers import EmployeeSerializer
class EmployeeViewSet(viewsets.ModelViewSet):
queryset = Employee.objects.all()
serializer_class = EmployeeSerializer
Edit employee_project/urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('employee.urls')), # Include app URLs here
]
Create employee/urls.py:
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import EmployeeViewSet
router = DefaultRouter()
router.register(r'employees', EmployeeViewSet)
urlpatterns = [
path('', include(router.urls)),
]
To start your backend server:
python manage.py runserver
Access the API at:
http://127.0.0.1:8000/api/employees/
You're able to:
{
"name": "John Doe",
"salary": "50000.00",
"location": "New York",
"mobile_no": "1234567890"
}
Add this to settings.py:
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.AllowAny',
]
}
Now the API is fully browsable and testable from browser.
Would you like me to upload this to GitHub or give you a ZIP file of the code?