0
0
Djangoframework~10 mins

Custom serializer fields in Django - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a custom serializer field that converts a value to uppercase.

Django
from rest_framework import serializers

class UpperCaseField(serializers.Field):
    def to_representation(self, value):
        return value.[1]()
Drag options to blanks, or click blank then click option'
Aupper
Blower
Ccapitalize
Dtitle
Attempts:
3 left
💡 Hint
Common Mistakes
Using lower() instead of upper()
Forgetting the parentheses after the method name
2fill in blank
medium

Complete the code to convert the input data to lowercase in the custom serializer field.

Django
class LowerCaseField(serializers.Field):
    def to_internal_value(self, data):
        return data.[1]()
Drag options to blanks, or click blank then click option'
Acapitalize
Blower
Cstrip
Dtitle
Attempts:
3 left
💡 Hint
Common Mistakes
Using capitalize() which only changes the first letter
Using strip() which removes whitespace
3fill in blank
hard

Fix the error in the custom field to correctly raise a validation error when input is not a string.

Django
from rest_framework import serializers

class StringField(serializers.Field):
    def to_internal_value(self, data):
        if not isinstance(data, str):
            raise serializers.[1]('Expected a string')
        return data
Drag options to blanks, or click blank then click option'
APermissionDenied
BParseError
CNotFound
DValidationError
Attempts:
3 left
💡 Hint
Common Mistakes
Using exceptions meant for HTTP errors like NotFound or PermissionDenied
Using ParseError which is for parsing issues
4fill in blank
hard

Fill both blanks to create a custom field that converts a datetime object to ISO format string and parses it back.

Django
import datetime
from rest_framework import serializers

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

    def to_internal_value(self, data):
        return datetime.datetime.[2](data)
Drag options to blanks, or click blank then click option'
Aisoformat
Bfromisoformat
Cstrftime
Dstrptime
Attempts:
3 left
💡 Hint
Common Mistakes
Using strftime and strptime without correct format strings
Mixing up the method names
5fill in blank
hard

Fill all three blanks to create a custom serializer field that validates an integer is positive and returns it.

Django
from rest_framework import serializers

class PositiveIntegerField(serializers.Field):
    def to_internal_value(self, data):
        value = int(data)
        if value [1] 0:
            raise serializers.ValidationError('[2] must be positive')
        return value

    def to_representation(self, value):
        return [3]
Drag options to blanks, or click blank then click option'
A<=
BValue
Cvalue
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '<=' causing zero to pass
Using lowercase 'value' in error message
Returning data instead of value in to_representation