Serializers in Django REST Framework
In this tutorial, we'll learn about serializers, their usage, and various techniques to handle serialization and deserialization in Django REST Framework.
What are Serializers in Django REST Framework?
Serializers are a crucial component of Django REST Framework (DRF) that enables easy conversion of complex data types like - querysets and model instances to native Python datatypes that can be rendered into JSON or other content types, and vice versa.
What is Serialization Class in DRF?
In Django REST Framework, the serializer class provides a powerful and generic way to write logic for the conversion of complex data types into Python native data types.
The Serialization class includes the following basic elements.
-
Fields: Serializer classes define fields that determine how data is serialized and deserialized.
-
Serialization and Deserialization Methods: Serializers include methods for converting data between Python objects and serialized representations.
-
Data Validation: Serializer fields can have validation rules to ensure data integrity.
-
Relationship Handling: Serializers handle relationships between models and represent related objects within the serialized output.
-
Customization: Serializer classes allow customization through methods like create() and update() for controlling object creation and updating.
Example -
In this example, we'll try to convert the data saved in our models to JSON using DRF serializers.
Here, we have added some data to our model.
Now, we'll create a serializers.py file and add the serializer class to that. Use the code given below to convert the models data to JSON using DRF Serializers,
serializers.py
from rest_framework import serializers
from .models import Students
class StudentsSerializer(serializers.ModelSerializer):
class Meta:
model = Students
fields = ['id', 'Name', 'Roll', 'City']
-
Here First, we import the necessary modules: serializers from Django REST Framework and the Students model from my app.
-
Then, we define a serializer class called StudentsSerializer by inheriting from serializers.ModelSerializer.
-
In the Meta class, we set the model attribute to Students, indicating that this serializer is associated with the Students model.
-
we specify the fields attribute to include only the desired fields from the Students model for serialization.
views.py
from .models import Students
from .serializers import StudentsSerializer
from rest_framework.renderers import JSONRenderer
students_data = Students.objects.all()
serializer = StudentsSerializer(students_data, many=True)
serialized_data = JSONRenderer().render(serializer.data)
print(serialized_data)
Here, we wrote simple logic to display the converted data on the terminal. We have used JSONRenderer() to convert the data from Python's native type to JSON.
Output
b'[{"id":3,"Name":"Raghav","Roll":21888345,"City":"Indore"},{"id":4,"Name":"Sneha","Roll":134888345,"City":"Jaipur"},{"id":5,"Name":"Mohak","Roll":13455589,"City":"Bhopal"}]'