A comprehensive Django REST Framework (DRF) project covering API development concepts from basics to advanced topics including serializers, views, viewsets, filtering, pagination, and nested relationships.
This project demonstrates various Django REST Framework concepts and patterns learned through hands-on implementation. It includes multiple models (Student, Employee, Blog, Comment) and showcases different approaches to building RESTful APIs.
- Python 3.14.0
- Django 5.2.7
- Django REST Framework 3.16.1
- django-filter (for advanced filtering)
- SQLite (database)
- Python 3.14.0 installed on your system
- pip (Python package manager)
-
Clone the repository (or navigate to the project directory)
cd drf-project -
Create and activate virtual environment
# On Windows python -m venv env .\env\Scripts\activate # On macOS/Linux python3 -m venv env source env/bin/activate
-
Install dependencies
pip install django djangorestframework django-filter
-
Configure Django settings
Add
rest_frameworktoINSTALLED_APPSindjango_rest_main/settings.py:INSTALLED_APPS = [ # ... other apps 'rest_framework', 'django_filters', 'students', 'employees', 'Blogs', 'api', ]
-
Run migrations
python manage.py makemigrations python manage.py migrate
-
Create superuser (optional, for admin access)
python manage.py createsuperuser
-
Run the development server
python manage.py runserver
The API will be available at
http://127.0.0.1:8000/
drf-project/
├── api/ # Main API app
│ ├── serializers.py # Serializers for all models
│ ├── views.py # API views (Function-based, Class-based, Viewsets)
│ ├── urls.py # URL routing
│ └── paginations.py # Custom pagination classes
├── students/ # Student model app
│ └── models.py # Student model
├── employees/ # Employee model app
│ ├── models.py # Employee model
│ └── filters.py # Custom filtering for employees
├── Blogs/ # Blog and Comment models app
│ ├── models.py # Blog and Comment models (with foreign key)
│ └── serializers.py # Nested serializers
├── django_rest_main/ # Main Django project settings
│ ├── settings.py # Django configuration
│ └── urls.py # Main URL configuration
└── manage.py # Django management script
- What is an API? - Understanding Application Programming Interfaces
- What is REST API? - RESTful architecture principles
- Software Installation - Setting up development environment
- Django Installation - Project initialization
- Django Rest Framework Installation - Installing and configuring DRF
- Web Application Endpoint - Creating simple endpoints
- Simple API Endpoint - First API endpoint implementation
- Create Model - Django model creation and database setup
- Manual Serialization - Serializing data without serializers
- Serializers Introduction - Understanding DRF serializers
- ModelSerializer - Using ModelSerializer for automatic serialization
- Serializer Validation - Data validation and error handling
- GET Method - Retrieving all objects using function-based views
- POST Method - Creating new objects
- Primary Key-Based Operations - Getting single object by ID
- PUT Method - Updating existing objects
- DELETE Method - Deleting objects
Example Endpoints:
GET /api/v1/students/- List all studentsPOST /api/v1/students/- Create a new studentGET /api/v1/students/<id>/- Get student by IDPUT /api/v1/students/<id>/- Update studentDELETE /api/v1/students/<id>/- Delete student
- Class-Based Views Introduction - Advantages of CBV over FBV
- Employee Model - Creating Employee model
- Employee Serializer - Serializer for Employee model
- GET All Employees - ListView implementation
- Creating Employee - CreateView implementation
- Getting Single Object - DetailView implementation
- Update And Delete Employee - UpdateView and DeleteView
- Mixins Overview - Understanding reusable mixin classes
- ListModelMixin - Listing objects
- CreateModelMixin - Creating objects
- RetrieveModelMixin - Retrieving single object
- UpdateModelMixin - Updating objects
- DestroyModelMixin - Deleting objects
- Generics Overview - Pre-built generic view classes
- ListCreateAPIView - Combined list and create operations
- RetrieveUpdateDestroyAPIView - Combined retrieve, update, and delete operations
- Viewsets Introduction - Understanding ViewSets and their benefits
- List And Create Data - Implementing list and create operations
- Retrieving Single Object - Implementing retrieve operation
- ModelViewSet - Complete CRUD operations in one class
- Router Configuration - Using DefaultRouter for automatic URL routing
Example:
# ViewSet automatically creates all CRUD endpoints
router.register('employees', views.EmployeesViewset)
# Creates:
# GET /api/v1/employees/ - List
# POST /api/v1/employees/ - Create
# GET /api/v1/employees/<id>/ - Retrieve
# PUT /api/v1/employees/<id>/ - Update
# DELETE /api/v1/employees/<id>/ - Delete- Nested Serializers Introduction - Handling relationships between models
- Blog And Comment Model - One-to-many relationship setup
- Creating Serializers - Parent and child serializers
- Nested Serializers Implementation - Embedding related objects in responses
- Primary Key-Based Operations On Blog Comment - CRUD operations on related models
Example:
class BlogSerializer(serializers.ModelSerializer):
comment = CommentSerializer(many=True, read_only=True)
class Meta:
model = Blog
fields = '__all__'- Pagination Overview - Breaking large datasets into pages
- Global Pagination - Setting pagination at project level
- Custom Pagination - Creating custom pagination classes
Custom Pagination Example:
class CustomPagination(PageNumberPagination):
page_size = 3
page_size_query_param = 'page_size'
page_query_param = 'page'- Filtering Overview - Filtering querysets
- Custom Filter By Designation - Filtering employees by designation
- Custom Filter By Name And ID - Multiple filter criteria
- Advanced Filtering - Complex filtering with django-filter
- Search Filter - Full-text search across multiple fields
- Ordering Filter - Sorting results by fields
Filter Examples:
# Custom filter
filterset_class = EmployeeFilter
# Search filter
search_fields = ['blog_title', 'blog_body']
# Ordering filter
ordering_fields = ['id']- student_id: CharField
- Name: CharField
- Branch: CharField
- created_at: DateTimeField (auto)
- updated_at: DateTimeField (auto)- emp_id: CharField
- emp_name: CharField
- Designation: CharField
- created_at: DateTimeField (auto)
- updated_at: DateTimeField (auto)- blog_title: CharField
- blog_body: TextField- blog: ForeignKey (Blog)
- comment: TextFieldGET /api/v1/students/- List all studentsPOST /api/v1/students/- Create studentGET /api/v1/students/<id>/- Get student by IDPUT /api/v1/students/<id>/- Update studentDELETE /api/v1/students/<id>/- Delete student
GET /api/v1/employees/- List all employees (with pagination & filtering)POST /api/v1/employees/- Create employeeGET /api/v1/employees/<id>/- Get employee by IDPUT /api/v1/employees/<id>/- Update employeePATCH /api/v1/employees/<id>/- Partial updateDELETE /api/v1/employees/<id>/- Delete employee
Filter Parameters:
?Designation=<value>- Filter by designation?emp_name=<value>- Filter by name (contains)?id_min=<value>&id_max=<value>- Filter by ID range
GET /api/v1/blogs/- List all blogs (with search & ordering)POST /api/v1/blogs/- Create blogGET /api/v1/blogs/<id>/- Get blog by ID (with nested comments)PUT /api/v1/blogs/<id>/- Update blogDELETE /api/v1/blogs/<id>/- Delete blog
Search Parameters:
?search=<query>- Search in blog_title and blog_body?ordering=<field>- Order by field (e.g.,?ordering=id)
GET /api/v1/comments/- List all commentsPOST /api/v1/comments/- Create commentGET /api/v1/comments/<id>/- Get comment by IDPUT /api/v1/comments/<id>/- Update commentDELETE /api/v1/comments/<id>/- Delete comment
Navigate to http://127.0.0.1:8000/api/v1/students/ in your browser to see the API response.
# Get all students
curl http://127.0.0.1:8000/api/v1/students/
# Create a student
curl -X POST http://127.0.0.1:8000/api/v1/students/ \
-H "Content-Type: application/json" \
-d '{"student_id": "S001", "Name": "John Doe", "Branch": "CS"}'
# Get employee with filters
curl "http://127.0.0.1:8000/api/v1/employees/?Designation=Developer&page=1"DRF provides a browsable API interface. Visit any endpoint in your browser to see an interactive API explorer.
- Serializers: Convert complex data types to/from JSON
- Function-Based Views: Simple view functions with decorators
- Class-Based Views: Reusable view classes
- Mixins: Reusable functionality for views
- Generic Views: Pre-built views for common patterns
- ViewSets: Actions-based views that work with routers
- ModelViewSet: Complete CRUD operations
- Nested Serializers: Handle model relationships
- Pagination: Manage large datasets
- Filtering: Filter querysets by various criteria
- Search: Full-text search across fields
- Ordering: Sort results dynamically
- ✅ Model serializers for automatic field handling
- ✅ Proper HTTP status codes
- ✅ Error handling and validation
- ✅ RESTful URL patterns
- ✅ Separation of concerns (models, serializers, views)
- ✅ Reusable pagination classes
- ✅ Custom filtering for complex queries
- ✅ Nested serializers for relationships
Consider exploring:
- Authentication and Permissions
- Token Authentication
- JWT Authentication
- API Versioning
- Throttling
- API Documentation with drf-yasg or drf-spectacular
- Testing with Django TestCase
- Caching strategies
- API rate limiting
This project is for educational purposes.
Created as a learning project to master Django REST Framework concepts.
Happy Coding! 🎉