Django REST Framework Q&A
Django REST Framework Q&A
Here are some common Django REST Framework (DRF) interview questions along with their
answers:
Answer: Django REST Framework (DRF) is a powerful and flexible toolkit for building Web APIs. It
allows you to create RESTful APIs using Django, offering features such as serialization, authentication,
and view sets, making API development straightforward.
Answer: Serializers in DRF are used to convert complex data types such as Django models or
QuerySets into JSON or XML format. They also perform deserialization, i.e., transforming incoming
JSON or XML into Django objects for use in the backend.
python
Answer:
APIView: A base class for all views in DRF. It provides the most control but requires more work,
such as manually defining `get()`, `post()`, etc.
ViewSet: It automatically defines routes for common actions like list, create, retrieve, update,
and delete (CRUD operations), reducing code duplication.
python
# APIView Example
class BookList(APIView):
def get(self, request):
books = Book.objects.all()
serializer = BookSerializer(books, many=True)
return Response(serializer.data)
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 1/18
# ViewSet Example
class BookViewSet(viewsets.ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer
Answer: Mixins in DRF are reusable classes that provide default implementations for actions like
`list`, `create`, `retrieve`, `update`, and `destroy`. You can combine these mixins with `APIView` to
create views for basic CRUD operations.
python
Answer: A QuerySet in Django represents a collection of objects from the database. In DRF, QuerySets
are used in views to fetch data, which is then serialized before sending it to the client.
python
class BookViewSet(viewsets.ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer
Answer: DRF supports multiple authentication methods like Basic Authentication, Token
Authentication, Session Authentication, and OAuth. You can configure the authentication classes in
your settings or views.
python
class BookViewSet(viewsets.ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 2/18
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]
Answer: Pagination in DRF refers to splitting a large list of results into smaller "pages". You can define
global pagination settings or customize them per view.
python
# settings.py
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10,
}
class BookList(generics.ListAPIView):
queryset = Book.objects.all()
serializer_class = BookSerializer
pagination_class = LimitOffsetPagination
Answer: Permissions in DRF define the level of access a user has to a resource. Common permissions
include `IsAuthenticated`, `IsAdminUser`, and custom permissions.
python
class BookViewSet(viewsets.ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer
permission_classes = [IsAuthenticated]
Answer:
`@api_view`: A function-based view decorator in DRF, used for simpler use cases.
`APIView`: A class-based view that offers more flexibility and scalability.
python
@api_view(['GET'])
def book_list(request):
books = Book.objects.all()
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 3/18
serializer = BookSerializer(books, many=True)
return Response(serializer.data)
Answer: Throttling is used to control the rate of requests a client can make to an API. You can set
different throttling policies, like per-user, per-anonymous user, etc.
python
# settings.py
REST_FRAMEWORK = {
'DEFAULT_THROTTLE_CLASSES': [
'rest_framework.throttling.UserRateThrottle',
'rest_framework.throttling.AnonRateThrottle',
],
'DEFAULT_THROTTLE_RATES': {
'user': '100/day',
'anon': '10/hour',
}
}
Answer: DRF provides custom exception handling through the `@api_view` decorator or class-based
views using the `handle_exception` method. You can also use middleware or define custom exception
handling globally.
python
# settings.py
REST_FRAMEWORK = {
'EXCEPTION_HANDLER': 'my_project.my_app.utils.custom_exception_handler'
}
Answer: DRF provides built-in support for filtering using the `django-filter` package. You can filter
QuerySets based on specific fields in your views or use filter backends.
python
class BookViewSet(viewsets.ModelViewSet):
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 4/18
queryset = Book.objects.all()
serializer_class = BookSerializer
filter_backends = [DjangoFilterBackend]
filterset_fields = ['author', 'published_date']
These questions cover various aspects of the Django REST Framework, from basic to intermediate
topics. Interviewers often focus on how well you can explain concepts and apply them in practical
situations.
more questions
Answer: `BrowsableAPIRenderer` is a renderer class in DRF that provides a web-browsable interface for
your API. It allows developers and users to interact with the API through a user-friendly HTML
interface, making it easier to test endpoints without the need for external tools like Postman.
python
# settings.py
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': [
'rest_framework.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
],
}
Answer:
ViewSets: Provide a way to combine the logic for a set of related views in a single class. They
automatically handle standard actions like `list`, `create`, `retrieve`, `update`, and `destroy`.
This reduces boilerplate code and simplifies URL routing using routers.
python
class AuthorViewSet(viewsets.ModelViewSet):
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 5/18
queryset = Author.objects.all()
serializer_class = AuthorSerializer
Generic Views: Offer base classes for building views with common patterns, such as listing or
creating objects. They provide more granular control compared to ViewSets and are suitable
when you need custom behavior.
python
class AuthorList(generics.ListCreateAPIView):
queryset = Author.objects.all()
serializer_class = AuthorSerializer
Answer: Nested serializers allow you to represent related objects within a serializer. This is useful for
displaying related data in a single API response.
python
class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = ['id', 'title']
class AuthorSerializer(serializers.ModelSerializer):
books = BookSerializer(many=True, read_only=True)
class Meta:
model = Author
fields = ['id', 'name', 'books']
To handle writable nested serializers (e.g., creating or updating related objects), you need to override
the `create` and `update` methods:
python
class AuthorSerializer(serializers.ModelSerializer):
books = BookSerializer(many=True)
class Meta:
model = Author
fields = ['id', 'name', 'books']
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 6/18
16. What is the role of Routers in DRF?
Answer: Routers in DRF automatically generate URL configurations for ViewSets, reducing the need to
manually define URL patterns for each action. They map HTTP methods to ViewSet actions like `list`,
`create`, `retrieve`, etc.
python
router = routers.DefaultRouter()
router.register(r'books', BookViewSet)
# urls.py
from django.urls import path, include
urlpatterns = [
path('', include(router.urls)),
]
python
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.TokenAuthentication',
],
}
python
class CustomTokenAuthentication(BaseAuthentication):
def authenticate(self, request):
token = request.headers.get('X-Custom-Token')
if not token:
return None
try:
user_token = Token.objects.get(key=token)
except Token.DoesNotExist:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 7/18
raise AuthenticationFailed('Invalid token')
return (user_token.user, token)
python
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'myapp.authentication.CustomTokenAuthentication',
],
}
18. What are Signals in Django, and how can they be used in DRF?
Answer: Signals in Django are a way to allow decoupled applications to get notified when certain
events occur. They are useful for executing certain actions automatically in response to events like
model creation, updates, or deletions.
Using Signals in DRF:
For example, you might want to send a notification or update related models whenever a new API
object is created.
python
# signals.py
from django.db.models.signals import post_save
from django.dispatch import receiver
from myapp.models import Book
from myapp.notifications import send_new_book_notification
@receiver(post_save, sender=Book)
def notify_new_book(sender, instance, created, **kwargs):
if created:
send_new_book_notification(instance)
Connecting Signals: Ensure that the signals are connected when the app is ready.
python
# apps.py
from django.apps import AppConfig
class MyAppConfig(AppConfig):
name = 'myapp'
def ready(self):
import myapp.signals
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 8/18
Answer: API versioning allows you to manage changes and updates to your API without disrupting
existing clients. DRF supports several versioning schemes:
python
# urls.py
from django.urls import path, include
urlpatterns = [
path('api/v1/', include('myapp.api.v1.urls')),
path('api/v2/', include('myapp.api.v2.urls')),
]
python
# settings.py
REST_FRAMEWORK = {
'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.QueryParameterVersioning',
'ALLOWED_VERSIONS': ['1.0', '2.0'],
'DEFAULT_VERSION': '1.0',
}
python
# settings.py
REST_FRAMEWORK = {
'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.HeaderVersioning',
}
python
# urls.py
from django.urls import path, include
urlpatterns = [
path('api/', include(('myapp.api.v1.urls', 'v1'), namespace='v1')),
path('api/', include(('myapp.api.v2.urls', 'v2'), namespace='v2')),
]
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 9/18
python
class ExampleView(APIView):
def get(self, request, format=None):
version = request.version
return Response({'version': version})
Answer: Caching can improve the performance of your API by storing frequently accessed data. You
can implement caching in DRF using Django’s caching framework.
Steps to Implement Caching:
python
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}
python
@api_view(['GET'])
@cache_page(60*15) # Cache for 15 minutes
def cached_book_list(request):
books = Book.objects.all()
serializer = BookSerializer(books, many=True)
return Response(serializer.data)
3. Cache at the ViewSet Level: Override the `list` method and apply caching.
python
class BookViewSet(viewsets.ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer
@method_decorator(cache_page(60*15))
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 10/18
def list(self, request, *args, **kwargs):
return super().list(request, *args, **kwargs)
21. What are Hyperlinked Serializers in DRF and how do they differ from standard
serializers?
Example:
python
class AuthorSerializer(serializers.HyperlinkedModelSerializer):
books = serializers.HyperlinkedRelatedField(
many=True,
read_only=True,
view_name='book-detail'
)
class Meta:
model = Author
fields = ['url', 'id', 'name', 'books']
class BookSerializer(serializers.HyperlinkedModelSerializer):
author = serializers.HyperlinkedRelatedField(
queryset=Author.objects.all(),
view_name='author-detail'
)
class Meta:
model = Book
fields = ['url', 'id', 'title', 'author']
URL Configuration: Ensure that your routers are set up with appropriate view names.
python
router = routers.DefaultRouter()
router.register(r'authors', AuthorViewSet)
router.register(r'books', BookViewSet)
urlpatterns = [
path('', include(router.urls)),
]
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 11/18
22. How do you implement Throttling in DRF and what types are available?
Answer: Throttling controls the rate of API requests to prevent abuse and ensure fair usage.
Types of Throttles:
Implementation Steps:
python
REST_FRAMEWORK = {
'DEFAULT_THROTTLE_CLASSES': [
'rest_framework.throttling.UserRateThrottle',
'rest_framework.throttling.AnonRateThrottle',
],
'DEFAULT_THROTTLE_RATES': {
'user': '1000/day',
'anon': '100/day',
'burst': '200/minute',
}
}
2. Apply ScopedRateThrottle: Use scopes to define specific rates for different views.
python
class BookViewSet(viewsets.ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer
throttle_classes = [ScopedRateThrottle]
throttle_scope = 'burst'
python
class CustomThrottle(BaseThrottle):
def allow_request(self, request, view):
# Implement custom logic
return True
def wait(self):
return None
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 12/18
23. What is `django-filter` and how is it used in DRF?
Answer: `django-filter` is a Django application for allowing users to filter querysets dynamically
based on GET parameters. In DRF, it integrates seamlessly to provide robust filtering capabilities.
Installation:
bash
Configuration:
python
INSTALLED_APPS = [
...
'django_filters',
]
python
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend']
}
Usage in Views:
python
class BookViewSet(viewsets.ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer
filter_backends = [DjangoFilterBackend]
filterset_fields = ['author', 'genre', 'published_date']
python
import django_filters
from myapp.models import Book
class BookFilter(django_filters.FilterSet):
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 13/18
title = django_filters.CharFilter(lookup_expr='icontains')
published_date = django_filters.DateFromToRangeFilter()
class Meta:
model = Book
fields = ['author', 'genre', 'title', 'published_date']
python
class BookViewSet(viewsets.ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer
filter_backends = [DjangoFilterBackend]
filterset_class = BookFilter
Answer: Handling file uploads in DRF involves configuring serializers and views to accept and process
file data.
Steps to Handle File Uploads:
python
class Document(models.Model):
title = models.CharField(max_length=100)
file = models.FileField(upload_to='documents/')
python
class DocumentSerializer(serializers.ModelSerializer):
class Meta:
model = Document
fields = ['id', 'title', 'file']
python
class DocumentViewSet(viewsets.ModelViewSet):
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 14/18
queryset = Document.objects.all()
serializer_class = DocumentSerializer
python
router = routers.DefaultRouter()
router.register(r'documents', DocumentViewSet)
urlpatterns = [
path('', include(router.urls)),
]
bash
Handling Large Files: For large file uploads, consider using chunked uploads or integrating with
cloud storage services like AWS S3.
25. What are Signals in DRF and how do they differ from Django Signals?
Answer: This appears to be a duplicate or confusion between DRF and Django signals. To clarify:
Django Signals: Django provides a built-in signal dispatcher that allows decoupled applications to get
notified when certain actions occur (e.g., model creation).
DRF Signals: DRF does not have its own signal system separate from Django’s. Instead, developers
use Django signals within DRF projects to handle events related to API operations.
Example Use Case: Using Django’s `post_save` signal to perform actions after an API creates a new
object.
python
@receiver(post_save, sender=Book)
def after_book_save(sender, instance, created, **kwargs):
if created:
# Perform action, e.g., send notification
pass
python
class IPRateThrottle(SimpleRateThrottle):
scope = 'ip'
2. Configure Throttle Rates: Add the custom throttle to `DEFAULT_THROTTLE_CLASSES` and define its
rate.
python
REST_FRAMEWORK = {
'DEFAULT_THROTTLE_CLASSES': [
'myapp.throttling.IPRateThrottle',
],
'DEFAULT_THROTTLE_RATES': {
'ip': '100/day',
}
}
3. Apply Throttle to Views: Ensure that your views use the throttle classes.
python
class BookViewSet(viewsets.ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer
throttle_classes = [IPRateThrottle]
Note: `get_ident(request)` retrieves the client’s IP address. Ensure your application correctly handles
cases where the app is behind proxies or load balancers by configuring `USE_X_FORWARDED_HOST` and
related settings.
27. Explain how to use JWT (JSON Web Tokens) with DRF.
Answer: JWT is a popular method for authenticating API requests. To use JWT with DRF, you typically
integrate the `djangorestframework-simplejwt` package.
Steps to Implement JWT Authentication:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 16/18
bash
python
# settings.py
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
),
}
python
urlpatterns = [
path('api/token/', TokenObtainPairView.as
Answer: Nested serializers allow you to represent related objects within a serializer. This is useful for
displaying related data in a single API response.
python
class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = ['id', 'title']
class AuthorSerializer(serializers.ModelSerializer):
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 17/18
books = BookSerializer(many=True, read_only=True)
class Meta:
model = Author
fields = ['id', 'name', 'books']
To handle writable nested serializers (e.g., creating or updating related objects), you need to override
the `create` and `update` methods in the parent serializer:
python
class AuthorSerializer(serializers.ModelSerializer):
books = BookSerializer(many=True)
class Meta:
model = Author
fields = ['id', 'name', 'books']
# Update books
for book_data in books_data:
book = instance.books.get(id=book_data.get('id'))
book.title = book_data.get('title', book.title)
book.save()
return instance
In this example, the `AuthorSerializer` can create and update authors along with their associated
books by handling nested data properly.
ChatGPT can make mistakes. Check important info.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 18/18