0
0
Djangoframework~20 mins

Custom serializer fields in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Serializer Fields Master
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 custom serializer field?
Consider this Django REST Framework serializer field that converts a string to uppercase during serialization.
What will be the serialized output for the input {'name': 'alice'}?
Django
from rest_framework import serializers

class UpperCaseField(serializers.Field):
    def to_representation(self, value):
        return value.upper()

class UserSerializer(serializers.Serializer):
    name = UpperCaseField()

serializer = UserSerializer({'name': 'alice'})
print(serializer.data)
ARaises AttributeError
B{'name': 'alice'}
C{'name': 'Alice'}
D{'name': 'ALICE'}
Attempts:
2 left
💡 Hint
Look at what the to_representation method does to the input value.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this custom serializer field
Which option correctly fixes the syntax error in this custom serializer field code?
Django
from rest_framework import serializers

class CustomField(serializers.Field):
    def to_representation(self, value)
        return str(value) + '!'
AAdd a colon after the method definition: def to_representation(self, value):
BChange 'def' to 'function' in the method definition
CRemove 'self' parameter from the method
DIndent the return statement one level less
Attempts:
2 left
💡 Hint
Python method definitions require a colon at the end.
state_output
advanced
2:00remaining
What is the value of 'data' after serialization?
Given this serializer with a custom field that reverses strings during serialization, what is the value of serializer.data?
Django
from rest_framework import serializers

class ReverseField(serializers.Field):
    def to_representation(self, value):
        return value[::-1]

class MessageSerializer(serializers.Serializer):
    message = ReverseField()

serializer = MessageSerializer({'message': 'hello'})
data = serializer.data
A{'message': 'olleh'}
BRaises TypeError
C{'message': 'HELLO'}
D{'message': 'hello'}
Attempts:
2 left
💡 Hint
The to_representation method reverses the string.
🔧 Debug
advanced
2:00remaining
Which option causes a runtime error when using this custom serializer field?
This custom field expects an integer but receives a string. Which option will cause a runtime error when serializing?
Django
from rest_framework import serializers

class DoubleIntegerField(serializers.Field):
    def to_representation(self, value):
        return value * 2

class NumberSerializer(serializers.Serializer):
    number = DoubleIntegerField()

serializer = NumberSerializer({'number': input_value})
output = serializer.data
Ainput_value = '5'
Binput_value = None
Cinput_value = 5
Dinput_value = 3.5
Attempts:
2 left
💡 Hint
Consider what happens when None is multiplied by 2.
🧠 Conceptual
expert
3:00remaining
Which option correctly implements a custom serializer field that validates input as a positive integer?
You want to create a custom serializer field that only accepts positive integers during input validation. Which option correctly implements the to_internal_value method to enforce this?
A
def to_internal_value(self, data):
    value = int(data)
    if value < 0:
        raise serializers.ValidationError('Must be positive')
    return value
B
def to_internal_value(self, data):
    if data > 0:
        return data
    else:
        raise serializers.ValidationError('Must be positive')
C
def to_internal_value(self, data):
    value = int(data)
    if value <= 0:
        raise serializers.ValidationError('Must be positive')
    return value
D
def to_internal_value(self, data):
    value = float(data)
    if value <= 0:
        raise serializers.ValidationError('Must be positive')
    return value
Attempts:
2 left
💡 Hint
Remember to convert input to int and check for positive values including zero.