To check how to setup Django RESt Framework and create a API visit – How to Create a basic API using Django Rest Framework ?
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.
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.
Using Serializer to serialize data
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.
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(firstname.lastname@example.org', content='foo bar') # serialize the data >>> serializer = CommentSerializer(comment) # print serialized data >>> serializer.data
Now let’s check ouutput for this,
We can convert this data to JSON or XML format using Python’s inbuilt functions or rest framework’s parsers.
Using Serializer to deserialize data
Deserialization is similar to Serialization. It means to convert the data from JSON format to a given data type. First we parse a stream into Python native datatypes… (define which datatype to deserialize to….)
Fist we need to convert this json data back to data that can be understood by serializer for deserializing,
and Now let’s deserialize the data back to its original state
Let’s check output and if data has been deserialized –
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Date and time fields in serializers - Django REST Framework
- Boolean Fields in Serializers - Django REST Framework
- String Fields in Serializers - Django REST Framework
- URL fields in serializers - Django REST Framework
- File upload Fields in Serializers - Django REST Framework
- ListField in serializers - Django REST Framework
- IPAddressField in serializers - Django REST Framework
- Numeric fields in serializers - Django REST Framework
- Choice Selection Fields in serializers - Django REST Framework
- ModelSerializer in serializers - Django REST Framework
- HiddenField in serializers - Django REST Framework
- JSONField in serializers - Django REST Framework
- DictField in serializers - Django REST Framework
- HyperlinkedModelSerializer in serializers - Django REST Framework
- Serializers - Django REST Framework
- Implement Token Authentication using Django REST Framework
- How to Create a basic API using Django Rest Framework ?
- Django REST Framework Installation
- Core arguments in serializer fields - Django REST Framework
- Serializer Fields - Django REST Framework
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.