Python > Advanced Topics and Specializations > Specific Applications (Overview) > Web Development (Flask, Django, FastAPI)
Django REST Framework with Class-Based Views
This snippet demonstrates how to create a simple REST API using Django REST Framework (DRF) and class-based views. DRF provides powerful tools for building RESTful APIs in Django, including serializers, authentication, and permissions.
Code Snippet (models.py)
This defines a simple Item
model with a name
and description
.
# models.py
from django.db import models
class Item(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
def __str__(self):
return self.name
Code Snippet (serializers.py)
This defines a serializer that converts Item
model instances to JSON and vice versa.
# serializers.py
from rest_framework import serializers
from .models import Item
class ItemSerializer(serializers.ModelSerializer):
class Meta:
model = Item
fields = ['id', 'name', 'description']
Code Snippet (views.py)
This defines two class-based views: ItemListCreate
for listing and creating items, and ItemRetrieveUpdateDestroy
for retrieving, updating, and deleting individual items.
# views.py
from rest_framework import generics
from .models import Item
from .serializers import ItemSerializer
class ItemListCreate(generics.ListCreateAPIView):
queryset = Item.objects.all()
serializer_class = ItemSerializer
class ItemRetrieveUpdateDestroy(generics.RetrieveUpdateDestroyAPIView):
queryset = Item.objects.all()
serializer_class = ItemSerializer
Code Snippet (urls.py)
This maps the API endpoints to the corresponding class-based views.
# urls.py
from django.urls import path
from . import views
urlpatterns = [
path('items/', views.ItemListCreate.as_view(), name='item-list-create'),
path('items/<int:pk>/', views.ItemRetrieveUpdateDestroy.as_view(), name='item-detail'),
]
Concepts Behind the Snippet
Key concepts in Django REST Framework include:
Real-Life Use Case Section
DRF is suitable for building complex REST APIs for web applications, mobile apps, and other services. For instance, an e-commerce platform could use DRF to expose APIs for managing products, orders, and user accounts.
Best Practices
Interview Tip
When discussing DRF in an interview, be prepared to explain the benefits of using serializers, the different types of class-based views, and how to implement authentication and permissions.
When to Use Them
Use Django REST Framework when you need to build a REST API with a robust and well-established framework and benefit from Django's ORM and other features.
Memory Footprint
The memory footprint of a DRF application depends on the complexity of your models, serializers, and views. Optimizing database queries and using caching can help reduce memory usage.
Alternatives
Alternatives to Django REST Framework include:
Pros
Cons
FAQ
-
What is a serializer in Django REST Framework?
A serializer in Django REST Framework converts model instances to JSON and vice versa. It also handles data validation and serialization. -
How do I implement authentication in DRF?
DRF provides various authentication schemes, including TokenAuthentication, SessionAuthentication, and JWTAuthentication. You can configure authentication schemes in theREST_FRAMEWORK
settings.