What is Data Serialization?

Nikita Jit
2 min readNov 1, 2020

As I was building my first project in Django, I came across something called data and image serialization which is achieved by importing the serializers from the rest framework. But when I went through the documentation, I could hardly make out anything and ended up leaving the topic. But now when I am giving it a try one more time with different resources, I found it to be an extremely easy concept to understand.

Let me begin by describing the function of a serializer class in Django. The main function of serializers is to convert the models in Django into JSON format. Whenever a user requests data from a web page, we send them the data in the basic HTML format using the HTTPResponse. But we don’t want to send them the HTML which is only when we want some information from the browser. We want to send them the JSON data. And this is exactly where the serializer class comes into play.

The serializers help in saving the data in a format that we can transfer. Suppose we want to save the python objects into a drive and give it to our friend, we can’t do so easily without the help of serializers which convert it into JSON format.

Now in order to use the serializers, we need to create a serializer.py file inside the app in our Django project. Then we need the import the particular model class or function, whose data we want to serialize. Now we need to create a new class whose name would be in the format ‘modelnameSerializer’ and we need to inherit the class of serializers.ModelSerializer. Then inside the same class, we need to create a meta class that will contain two fields. The first one will be the name of the model in the model variable and the second thing is the attributes that we want to return in the JSON format. In the second part, we need to create a variable called fields that will contain either all the attributes of the model class for eg, fields = (‘name’, ‘address’, ‘DOB’) or simply the desired attributes only.

That’s all for today. Happy Coding!

--

--

Nikita Jit

CS Undergrad | ML enthusiast | Bibliophile | Exploring the technical side of writing