When building APIs in Django REST Framework (DRF), you can create endpoints using either Function-Based Views (FBVs) or Class-Based Views (CBVs). Both approaches can handle API requests, but they differ in structure, flexibility, and scalability.
Understanding the differences will help you choose the right approach for your project.
What are Function-Based Views?
Function-Based Views use Python functions to handle HTTP requests. They are simple and easy to understand, making them ideal for small APIs and beginners.
Example: Function-Based View
from rest_framework.decorators import api_view
from rest_framework.response import Response
@api_view(['GET'])
def employee_list(request):
return Response({
"message": "Employee List"
})
URL Configuration
from django.urls import path
from .views import employee_list
urlpatterns = [
path('employees/', employee_list),
]
Request:
GET /api/employees/
Response:
{
"message": "Employee List"
}
What are Class-Based Views?
Class-Based Views organize request handling inside a class. Different HTTP methods are implemented as separate methods such as get(), post(), put(), and delete().
Example: Class-Based View
from rest_framework.views import APIView
from rest_framework.response import Response
class EmployeeAPIView(APIView):
def get(self, request):
return Response({
"message": "Employee List"
})
URL Configuration
from django.urls import path
from .views import EmployeeAPIView
urlpatterns = [
path('employees/', EmployeeAPIView.as_view()),
]
Request:
GET /api/employees/
Response:
{
"message": "Employee List"
}
Handling Multiple HTTP Methods
Function-Based View
from rest_framework.decorators import api_view
from rest_framework.response import Response
@api_view(['GET', 'POST'])
def employee_view(request):
if request.method == 'GET':
return Response({"message": "Employee List"})
if request.method == 'POST':
return Response({"message": "Employee Created"})
Class-Based View
from rest_framework.views import APIView
from rest_framework.response import Response
class EmployeeAPIView(APIView):
def get(self, request):
return Response({"message": "Employee List"})
def post(self, request):
return Response({"message": "Employee Created"})
Notice how CBVs separate each HTTP method into its own function, making the code cleaner and easier to maintain.
Applying Permissions
Function-Based View
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
@api_view(['GET'])
@permission_classes([IsAuthenticated])
def profile(request):
return Response({
"user": request.user.username
})
Class-Based View
from rest_framework.views import APIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
class ProfileAPIView(APIView):
permission_classes = [IsAuthenticated]
def get(self, request):
return Response({
"user": request.user.username
})
Advantages of Function-Based Views
- Simple and easy to understand
- Less code for small APIs
- Good for beginners
- Quick to implement
Disadvantages of Function-Based Views
- Difficult to manage large APIs
- Repeated code for multiple endpoints
- Less reusable
- Harder to extend and maintain
Advantages of Class-Based Views
- Better code organization
- Supports inheritance
- Easy to reuse functionality
- Built-in DRF generic views and viewsets
- Suitable for large applications
Disadvantages of Class-Based Views
- Slightly more complex for beginners
- More boilerplate code for simple APIs
Function-Based View vs Class-Based View
| Feature | Function-Based View | Class-Based View |
|---|---|---|
| Syntax | Simple | Structured |
| Learning Curve | Easy | Moderate |
| Code Reusability | Low | High |
| Scalability | Limited | Excellent |
| Inheritance Support | No | Yes |
| Best For | Small APIs | Medium & Large APIs |
Which One Should You Use?
Use Function-Based Views when:
- Building small APIs
- Learning DRF
- Creating simple endpoints
Use Class-Based Views when:
- Building production applications
- Creating complex APIs
- Reusing logic across multiple views
- Planning to use Generic Views or ViewSets
Conclusion
Both Function-Based Views and Class-Based Views are valid approaches in Django REST Framework. Function-Based Views are great for simplicity, while Class-Based Views provide better organization, reusability, and scalability for larger applications. In most real-world DRF projects, Class-Based Views are the preferred choice.