Serializers in Django REST Framework are responsible for converting objects into data types understandable by javascript and front-end frameworks. Serializer also provide deserialization, allowing parsed data to converted back into complex types, after first validating the incoming data. The serializers in REST framework work very similarly to Django’s Form and Model Form classes. The two major serializers that are most popularly used are ModelSerializer and HyperLinkedModelSerializer.

Serialization transforms data into a format that can stored or transmitted and then reconstructs it for use. DRF has the best-known serializers.

This article gives you a brief information about Serializer

Installation

Install Using pip

pip install djangorestframework

add ‘rest_framework to your INSTALLED_APPS setting

INSTALLED_APPS = [
    ...
    'rest_framework',
]

Creating and Using Serializers in Rest

Creating a basic Serializer

To create a basic serializer one needs to import serializers class from rest_framework and define fields for a serializer just like creating a form or model in Django. 

Like

from rest_framework import serializers

class CommentSerializer(serializers.Serializer):
    # initialize fields
    email = serializers.EmailField()
    content = serializers.CharField(max_length = 200)
    created = serializers.DateTimeField()

This way one can declare serializer for any particular entity or object based on fields required. Serializers can be used to serialize as well as deserialize the data.

This is pretty straightforward to do with simple data, but when you need to deal with complex objects made of complex attributes, this approach doesn’t scale well. You also need to handle the validation of different types of fields, and that’s a lot of work to do manually.

That’s where frameworks’ serializers are handy. They allow you to create serializers with little boilerplates that will work for your complex cases.

Using Serializer to serialize data in Rest

One can now use CommentSerializer to serialize a comment, or list of comments. Again, using the Serializer class looks a lot like using a Form class. Let’s create a Comment class first to create a object of type comment that can be understood by our serializer.  

from datetime import datetime

class Comment(object):
    def __init__(self, email, content, created = None):
        self.email = email
        self.content = content
        self.created = created or datetime.now()
# create a object
comment = Comment(email ='[email protected]', content ='foo bar')

Now that our object is ready, let’s try serializing this comment object. Run following command, 

Python manage.py shell

Now run the following code

# import comment serializer
>>> from apis.serializers import CommentSerializer

# import datetime for date and time
>>> from datetime import datetime

# create a object
>>> class Comment(object):
...     def __init__(self, email, content, created=None):
...         self.email = email
...         self.content = content
...         self.created = created or datetime.now()
... 

# create a comment object
>>> comment = Comment(email='[email protected]', content='foo bar')

# serialize the data
>>> serializer = CommentSerializer(comment)

# print serialized data
>>> serializer.data

We can get output as

{’email’:’[email protected]’,’content’:’foo bar’,’created’:’2022-12-04 12:35:46.375912′}

For More Information

https://github.com/saikumar248