0
0
Djangoframework~20 mins

Serializers for data conversion in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Serializer 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 serializer's data?
Given the Django REST Framework serializer below, what will be the output of serializer.data when serializing the user instance?
Django
from rest_framework import serializers

class UserSerializer(serializers.Serializer):
    id = serializers.IntegerField()
    username = serializers.CharField(max_length=100)
    is_active = serializers.BooleanField(default=True)

user = type('User', (), {'id': 1, 'username': 'alice', 'is_active': False})()
serializer = UserSerializer(user)
A{'id': 1, 'username': 'alice', 'is_active': False}
B{'id': '1', 'username': 'alice', 'is_active': False}
C{'id': 1, 'username': 'alice', 'is_active': True}
DRaises AttributeError because user is not a model instance
Attempts:
2 left
💡 Hint
Look at the values assigned to the user instance attributes and how the serializer reads them.
📝 Syntax
intermediate
2:00remaining
Which serializer code will correctly validate nested data?
You want to create a serializer that validates a nested dictionary for a blog post with an author. Which option correctly defines the nested serializer?
Django
from rest_framework import serializers

class AuthorSerializer(serializers.Serializer):
    name = serializers.CharField(max_length=100)

class PostSerializer(serializers.Serializer):
    title = serializers.CharField(max_length=200)
    author = ???  # Fill this line correctly
Aauthor = serializers.Serializer()
Bauthor = serializers.DictField()
Cauthor = serializers.CharField()
Dauthor = AuthorSerializer()
Attempts:
2 left
💡 Hint
Think about how to embed one serializer inside another for nested validation.
🔧 Debug
advanced
2:00remaining
Why does this serializer raise a ValidationError?
Consider this serializer and data. Why does calling serializer.is_valid() raise a ValidationError?
Django
from rest_framework import serializers

class ProductSerializer(serializers.Serializer):
    name = serializers.CharField(max_length=50)
    price = serializers.DecimalField(max_digits=5, decimal_places=2)

input_data = {'name': 'Pen', 'price': '12.345'}
serializer = ProductSerializer(data=input_data)
serializer.is_valid(raise_exception=True)
APrice has more decimal places than allowed (3 instead of 2)
BName exceeds max length of 50 characters
CMissing required field 'price'
DPrice is a string, should be a float
Attempts:
2 left
💡 Hint
Check the decimal places allowed in the DecimalField and the input value.
state_output
advanced
2:00remaining
What is the value of serializer.validated_data after validation?
Given this serializer and input data, what will serializer.validated_data contain after calling serializer.is_valid()?
Django
from rest_framework import serializers

class CommentSerializer(serializers.Serializer):
    text = serializers.CharField()
    likes = serializers.IntegerField(default=0)

input_data = {'text': 'Nice post!'}
serializer = CommentSerializer(data=input_data)
serializer.is_valid()
A{'text': 'Nice post!'}
B{'text': 'Nice post!', 'likes': None}
C{'text': 'Nice post!', 'likes': 0}
DRaises ValidationError because 'likes' is missing
Attempts:
2 left
💡 Hint
Consider how default values work in serializers when fields are missing in input.
🧠 Conceptual
expert
2:00remaining
Which statement about serializer create() method is true?
In Django REST Framework, when you override the create() method in a serializer, which statement is correct?
AIt automatically saves data to the database without needing to call <code>save()</code>
BIt must return the created model instance after saving the validated data
CIt is called before <code>is_valid()</code> to prepare data
DIt is only used for updating existing instances, not creating new ones
Attempts:
2 left
💡 Hint
Think about the role of create() in the serializer lifecycle during object creation.