When working with Django REST Framework (DRF), you’ll frequently use Serializer and ModelSerializer to convert data between Django models and JSON. While both serve the same purpose, they differ significantly in terms of functionality and code complexity.
Understanding when to use each one can make your API development faster and more maintainable.
What is a Serializer?
A Serializer is the basic serializer class in DRF. You must manually define every field and implement create() and update() methods if you want to save data.
Example Model
from django.db import models
class Employee(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField()
department = models.CharField(max_length=100)
def __str__(self):
return self.name
Serializer Example
from rest_framework import serializers
from .models import Employee
class EmployeeSerializer(serializers.Serializer):
id = serializers.IntegerField(read_only=True)
name = serializers.CharField(max_length=100)
email = serializers.EmailField()
department = serializers.CharField(max_length=100)
def create(self, validated_data):
return Employee.objects.create(**validated_data)
def update(self, instance, validated_data):
instance.name = validated_data.get('name', instance.name)
instance.email = validated_data.get('email', instance.email)
instance.department = validated_data.get('department', instance.department)
instance.save()
return instance
Notice that all fields and methods are written manually.
What is a ModelSerializer?
ModelSerializer automatically generates serializer fields based on a Django model. It also provides built-in implementations of create() and update() methods.
ModelSerializer Example
from rest_framework import serializers
from .models import Employee
class EmployeeSerializer(serializers.ModelSerializer):
class Meta:
model = Employee
fields = "__all__"
That’s all you need. DRF automatically handles field generation and object creation.
How ModelSerializer Works
Given this model:
class Employee(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField()
department = models.CharField(max_length=100)
DRF automatically creates serializer fields equivalent to:
name = serializers.CharField(max_length=100)
email = serializers.EmailField()
department = serializers.CharField(max_length=100)
without writing them manually.
Using Specific Fields
Instead of returning all model fields, you can specify which fields to expose.
class EmployeeSerializer(serializers.ModelSerializer):
class Meta:
model = Employee
fields = ['id', 'name', 'email']
Response:
{
"id": 1,
"name": "Tarun Kumar",
"email": "tarun@example.com"
}
Excluding Fields
You can exclude sensitive fields.
class EmployeeSerializer(serializers.ModelSerializer):
class Meta:
model = Employee
exclude = ['department']
Adding Extra Validation
Even with ModelSerializer, custom validation is possible.
from rest_framework import serializers
class EmployeeSerializer(serializers.ModelSerializer):
class Meta:
model = Employee
fields = "__all__"
def validate_name(self, value):
if len(value) < 3:
raise serializers.ValidationError(
"Name must contain at least 3 characters."
)
return value
Adding Custom Fields
You can include calculated fields using SerializerMethodField.
from rest_framework import serializers
class EmployeeSerializer(serializers.ModelSerializer):
full_info = serializers.SerializerMethodField()
class Meta:
model = Employee
fields = "__all__"
def get_full_info(self, obj):
return f"{obj.name} - {obj.department}"
Response:
{
"id": 1,
"name": "Tarun Kumar",
"department": "IT",
"full_info": "Tarun Kumar - IT"
}
Serializer vs ModelSerializer
| Feature | Serializer | ModelSerializer |
|---|---|---|
| Field Definition | Manual | Automatic |
| Validation | Manual | Automatic + Custom |
| create() Method | Manual | Auto Generated |
| update() Method | Manual | Auto Generated |
| Code Length | More | Less |
| Performance | Similar | Similar |
| Flexibility | High | High |
| Best For | Custom Data Structures | Django Models |
When to Use Serializer
Use Serializer when:
- Data is not coming from a Django model.
- You need complete control over fields.
- Building custom response formats.
- Working with external APIs.
- Handling complex business logic.
Example:
class LoginSerializer(serializers.Serializer):
email = serializers.EmailField()
password = serializers.CharField()
No model is required.
When to Use ModelSerializer
Use ModelSerializer when:
- Working directly with Django models.
- Building CRUD APIs.
- You want faster development.
- Standard model validation is sufficient.
Example:
class EmployeeSerializer(serializers.ModelSerializer):
class Meta:
model = Employee
fields = "__all__"
Advantages of ModelSerializer
- Less code
- Faster development
- Automatic validation
- Built-in create/update methods
- Easy maintenance
Advantages of Serializer
- Full customization
- Works without models
- Better for complex data transformations
- Ideal for non-database data
Conclusion
Both Serializer and ModelSerializer are powerful tools in Django REST Framework. If you’re working with Django models, ModelSerializer is usually the preferred choice because it reduces boilerplate code and speeds up development. Use Serializer when you need complete control or when your data doesn’t come from a Django model.