Challenge - 5 Problems
DRF Advanced Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this DRF serializer with nested relationships?
Given this Django REST Framework serializer code, what will be the JSON output when serializing a
Book instance with one Author?Django
from rest_framework import serializers class AuthorSerializer(serializers.Serializer): name = serializers.CharField() class BookSerializer(serializers.Serializer): title = serializers.CharField() author = AuthorSerializer() book_data = {'title': 'Deep Learning', 'author': {'name': 'Ian Goodfellow'}} serializer = BookSerializer(data=book_data) serializer.is_valid() print(serializer.data)
Attempts:
2 left
💡 Hint
Think about how nested serializers represent related objects in DRF.
✗ Incorrect
The nested serializer
AuthorSerializer correctly serializes the author field as a dictionary with the author's name. So the output includes the nested dictionary.❓ state_output
intermediate2:00remaining
What is the value of
validated_data after calling is_valid()?Consider this DRF serializer with a custom validation method. What will
serializer.validated_data contain after serializer.is_valid() is called?Django
from rest_framework import serializers class ProductSerializer(serializers.Serializer): name = serializers.CharField() price = serializers.FloatField() def validate_price(self, value): if value <= 0: raise serializers.ValidationError('Price must be positive') return value input_data = {'name': 'Laptop', 'price': 999.99} serializer = ProductSerializer(data=input_data) serializer.is_valid() print(serializer.validated_data)
Attempts:
2 left
💡 Hint
Validated data converts fields to correct types and applies validations.
✗ Incorrect
The price is positive, so validation passes and validated_data contains the input with correct types.
📝 Syntax
advanced2:00remaining
Which option correctly defines a DRF viewset with a custom action?
You want to add a custom action named
highlight to a DRF viewset that responds to GET requests. Which code snippet is correct?Django
from rest_framework import viewsets from rest_framework.decorators import action from rest_framework.response import Response class ArticleViewSet(viewsets.ModelViewSet): queryset = Article.objects.all() serializer_class = ArticleSerializer # Custom action here
Attempts:
2 left
💡 Hint
Custom actions need the detail and methods parameters correctly set.
✗ Incorrect
Option B correctly uses @action with detail=True and methods=['get'], matching the requirement.
🔧 Debug
advanced2:00remaining
What error does this DRF serializer raise?
This serializer tries to use a field that does not exist on the model. What error will it raise when instantiated?
Django
from rest_framework import serializers class UserSerializer(serializers.ModelSerializer): full_name = serializers.CharField() class Meta: model = User fields = ['username', 'email', 'full_name'] serializer = UserSerializer()
Attempts:
2 left
💡 Hint
ModelSerializer requires an instance or data argument when instantiated.
✗ Incorrect
Instantiating a ModelSerializer without instance or data raises TypeError about missing required positional argument.
🧠 Conceptual
expert2:00remaining
Why use advanced DRF features like throttling and pagination?
Which of the following best explains why advanced DRF features such as throttling and pagination are important in real-world APIs?
Attempts:
2 left
💡 Hint
Think about how APIs handle many users and large data sets.
✗ Incorrect
Throttling controls how often clients can make requests, preventing overload. Pagination limits data size per response, improving speed and usability.