0
0
Djangoframework~20 mins

Why advanced DRF features matter in Django - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DRF Advanced Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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)
A{'title': 'Deep Learning', 'author': {'name': 'Ian Goodfellow'}}
B{'title': 'Deep Learning', 'author': 'Ian Goodfellow'}
C{'title': 'Deep Learning', 'author': None}
DRaises a TypeError
Attempts:
2 left
💡 Hint
Think about how nested serializers represent related objects in DRF.
state_output
intermediate
2: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)
ARaises ValidationError
B{'name': 'Laptop', 'price': '999.99'}
C{'name': 'Laptop', 'price': 999.99}
D{}
Attempts:
2 left
💡 Hint
Validated data converts fields to correct types and applies validations.
📝 Syntax
advanced
2: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
A
@action(detail=False, methods=['post'])
def highlight(self, request):
    return Response({'highlighted': 'none'})
B
@action(detail=True, methods=['get'])
def highlight(self, request, pk=None):
    article = self.get_object()
    return Response({'highlighted': article.title.upper()})
C
@action(methods=['get'])
def highlight(self, request, pk=None):
    return Response({'highlighted': 'yes'})
D
@action(detail=True)
def highlight(self, request):
    return Response({'highlighted': 'no'})
Attempts:
2 left
💡 Hint
Custom actions need the detail and methods parameters correctly set.
🔧 Debug
advanced
2: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()
ARaises TypeError due to missing argument
BRaises AssertionError about missing field on model
CNo error, serializer works fine
DRaises ValidationError at runtime
Attempts:
2 left
💡 Hint
ModelSerializer requires an instance or data argument when instantiated.
🧠 Conceptual
expert
2: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?
AThey automatically generate frontend UI components for the API.
BThey simplify the code by removing the need for serializers.
CThey replace the need for database indexing and caching.
DThey improve API security and performance by limiting request rates and data size per response.
Attempts:
2 left
💡 Hint
Think about how APIs handle many users and large data sets.