Complete the code to define a custom serializer field that converts a value to uppercase.
from rest_framework import serializers class UpperCaseField(serializers.Field): def to_representation(self, value): return value.[1]()
The to_representation method converts the value to uppercase using the upper() string method.
Complete the code to convert the input data to lowercase in the custom serializer field.
class LowerCaseField(serializers.Field): def to_internal_value(self, data): return data.[1]()
The to_internal_value method processes input data. Using lower() converts the string to lowercase.
Fix the error in the custom field to correctly raise a validation error when input is not a string.
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
To signal invalid input in serializers, raise serializers.ValidationError.
Fill both blanks to create a custom field that converts a datetime object to ISO format string and parses it back.
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)
isoformat() converts datetime to ISO string. fromisoformat() parses ISO string back to datetime.
Fill all three blanks to create a custom serializer field that validates an integer is positive and returns it.
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]
The code checks if value <= 0 to raise an error. The error message uses 'Value'. The to_representation returns the integer value.