0
0
Djangoframework~10 mins

Custom serializer fields in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom serializer fields
Define custom field class
Override to_internal_value
Override to_representation
Use custom field in serializer
Serialize data -> calls to_representation
Deserialize input -> calls to_internal_value
This flow shows how to create a custom serializer field by defining a class, overriding methods to convert data in and out, then using it in a serializer for data transformation.
Execution Sample
Django
from rest_framework import serializers

class UpperCaseField(serializers.CharField):
    def to_representation(self, value):
        return value.upper()
Defines a custom serializer field that converts string output to uppercase when serializing.
Execution Table
StepActionInput ValueMethod CalledOutput/Result
1Create UpperCaseField instanceN/AConstructorField ready for use
2Serialize data with value 'hello''hello'to_representation'HELLO'
3Deserialize input 'world''world'to_internal_value (default CharField)'world'
4Serialize data with value 'Django''Django'to_representation'DJANGO'
5Deserialize input 'Test''Test'to_internal_value (default CharField)'Test'
💡 Serialization and deserialization complete using custom field methods
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5
valueN/A'hello''world''Django''Test'
serialized_outputN/A'HELLO'N/A'DJANGO'N/A
deserialized_outputN/AN/A'world'N/A'Test'
Key Moments - 2 Insights
Why does to_representation change the output but to_internal_value does not in this example?
Because to_representation is overridden to convert the string to uppercase during serialization (see steps 2 and 4), while to_internal_value uses the default CharField behavior for deserialization (steps 3 and 5).
What happens if you don't override to_internal_value in a custom field?
The field uses the parent class's to_internal_value method, so input data is processed normally without custom changes, as shown in steps 3 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output of serializing the value 'hello' at step 2?
A'Hello'
B'HELLO'
C'hello'
D'HELlo'
💡 Hint
Check the 'Output/Result' column at step 2 in the execution_table
At which step does deserialization of input 'world' occur?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look for 'to_internal_value' method calls in the execution_table
If you override to_internal_value to convert input to lowercase, how would the deserialized output change at step 5?
A'Test'
B'TEST'
C'test'
D'tEsT'
💡 Hint
Consider how to_internal_value transforms input before returning it
Concept Snapshot
Custom serializer fields in Django REST Framework let you control how data is converted in and out.
Override to_representation to change output format.
Override to_internal_value to customize input parsing.
Use your custom field in serializers like any other field.
This helps handle special data formats or validation cleanly.
Full Transcript
This lesson shows how to create and use custom serializer fields in Django REST Framework. You define a new field class by subclassing an existing field, then override to_representation to change how data is output during serialization, and optionally override to_internal_value to customize input parsing during deserialization. The example UpperCaseField converts strings to uppercase when serializing. The execution table traces creating the field, serializing values like 'hello' and 'Django' to uppercase, and deserializing inputs like 'world' and 'Test' using default behavior. Key points include understanding that to_representation affects output, while to_internal_value affects input, and that if you don't override to_internal_value, the default method is used. The visual quiz tests understanding of these method calls and their effects on data transformation. This approach helps you handle special data formats or validation needs in your APIs.