Serializers in DRF

What is a Serializer in DRF?

In simple words, a Serializer in Django REST Framework (DRF) is used to convert Django model data (Python objects) into JSON format so it can be easily shared over the internet via APIs.Similarly, when your API receives JSON data from a user, the Serializer converts it back into Python/Django objects so your application can understand and save it in the database.

Think of a Serializer as a translator — it helps two different systems (your Django backend and a frontend or external app) communicate in a common language (JSON).

Creating a Basic Serializer

We will now create a serializer for the Book model that we created earlier in models.py.

To do this, we need to create a new file named serializers.py inside the same app folder where you created your models.py file, which in our case is the books app.

So the folder structure will look like this:

 

books/

├── models.py

├── serializers.py  ← We’ll create this file now

├── views.py

└── …

books/serializers.py

from rest_framework import serializers

from .models import Book

class BookSerializer(serializers.ModelSerializer):

    class Meta:

        model = Book

        fields = ‘__all__’

Explanation:

// from rest_framework import serializers

This line imports the serializers module from the Django REST Framework. This module contains all the classes and tools we need to create serializers.

// from .models import Book

Here, we are importing our Book model from the models.py file that we created earlier. We need this because our serializer will be based on this model.

// class BookSerializer(serializers.ModelSerializer):

This line defines a new class called BookSerializer. It inherits from serializers.ModelSerializer, which means this serializer will automatically take fields from the Book model. It saves us from writing each field manually.

  // class Meta:

This is a nested class called Meta inside our BookSerializer. It’s used to give extra information to the serializer like:

  • Which model it should use
  • Which fields to include in the serialization

       // model = Book

Here we are telling the serializer that it should be connected to the Book model. So all the fields in the Book model will be considered in this serializer.

       // fields = ‘__all__’

This line tells the serializer to include all the fields from the Book model. So, fields like title, author, price, and published_date will be included automatically in our API.

If you only wanted to include specific fields, you could write something like:

fields = [‘title’, ‘author’]

But for now, we are using __all__ to include everything.

What is a ModelSerializer?

A ModelSerializer is a shortcut class provided by DRF. It automatically:

  • Creates serializer fields based on your model fields.
  • Handles data validation.
  • Makes your code much shorter and cleaner.

If we don’t use ModelSerializer, we would have to define each field manually, like this:

class BookSerializer(serializers.Serializer):

    title = serializers.CharField(max_length=100)

    author = serializers.CharField(max_length=100)

    price = serializers.DecimalField(max_digits=6, decimal_places=2)

    published_date = serializers.DateField()

This approach gives more control but also requires more effort. That’s why we prefer ModelSerializer when we are working with Django models.

Summary:

  • Serializers convert model data into JSON and JSON into model data.
  • ModelSerializer is an easy and faster way to build serializers for models.
  • We created BookSerializer using ModelSerializer for our Book model.
  • Next, we will use this serializer in our API views.

Exercise:

  • Create a serializer for the Student model.

  • Print the serialized data for a single student in the Django shell.

Bonus:

  • Convert the student object into JSON using the serializer.

Course Video in English