Enhancing Serialization in Django REST Framework: A Guide to Customizing Serializer Output

Table of contents

No heading

No headings in the article.

  1. Initial Serializer Implementation:
class ModelSetSerializer(serializers.ModelSerializer):
    exam = ExamSerializer()

    class Meta:
        model = ModelSet
        fields = ("id", "title", "is_free", "exam")


  • In this initial implementation, the ModelSetSerializer is defined as a subclass of serializers.ModelSerializer.

  • The exam field is associated with the ExamSerializer, which means it will serialize the related Exam object and return its representation.

  • The Meta class specifies the model to be serialized (ModelSet) and the fields to be included in the serialized output (id, title, is_free, and exam).

  1. Issue with Creating Instances: Before we move on to the updated serializer implementation, it's important to note that using the initial serializer implementation can cause issues when creating instances. It requires passing the whole exam object as a dictionary, rather than simply passing the exam ID. This can be problematic, especially when using CreateAPIView.

  2. Desired Output:

  "success": true,
  "data": {
    "id": 1,
    "title": "modelset 2080",
    "is_free": true,
    "exam": {


  • The desired output showcases the complete details of the exam field, including nested objects such as category.

  • It demonstrates the need to represent the exam field as a fully serialized object, rather than just the primary key.

  1. Updated Serializer Implementation:
class ModelSetSerializer(serializers.ModelSerializer):
    exam = serializers.PrimaryKeyRelatedField(queryset=Exam.objects.all())

    class Meta:
        model = ModelSet
        fields = ("id", "title", "is_free", "exam")

    def to_representation(self, instance):
        representation = super().to_representation(instance)
        representation['exam'] = ExamSerializer(instance.exam).data
        return representation


  • In the updated implementation, the exam field is changed to serializers.PrimaryKeyRelatedField.

  • This change allows the serializer to represent the exam field as the primary key (id), rather than serializing the complete Exam object.

  • The to_representation method is overridden to modify the serialized data before it is sent as a response.

  • Inside the to_representation method, the original representation is obtained using super().to_representation(instance).

  • Then, the exam field in the representation is replaced with the serialized data obtained from ExamSerializer(instance.exam).data.

By using the updated serializer implementation, you can achieve the desired output while still being able to pass just the exam ID when creating instances or using CreateAPIView.