Django REST
About Lesson

In this Django REST Framework course we are going to learn about Django REST Framework Serialization, before sending data to the client we need to serialize the data to JSON. the first thing we need to get started on our Web API is to provide a way of serializing and deserializing the data instances into representations such as json. We can do this by declaring serializers that work very similar to Django’s forms.

 

 

We have already installed our Django REST Framework, but we have not integrate that with our Django Project, open your settings.py and that this in your INSTALLED_APPS.

 

 

 

 

Create a new Python file at name of serializers.py. there are different serializers that you can use, we will do this step by step.

 

The first serializer class is serializer.Serializer, when you want to use this class in Django REST Framework Serialization you need to specify all the fields manually, for example if you have three fields in your model, than you need to specify all of the fields in your serializers.py file.

 

The first part of the serializer class defines the fields that get serialized/deserialized. The create() and update() methods define how fully fledged instances are created or modified when calling serializers.save()

A serializer class is very similar to a Django Form class, and includes similar validation flags on the various fields, such as required, max_length and default.

 

 

OK now open django shell using this command.

 

 

In django shell first of all you need these imports.

 

 

Now we need to create an article and save that.

 

If you check your django admin panel, you can see that i have a new article. also you can add as much as articles you want.

 

 

Now let’s just serialize the article, we are going to open django shell.

 

 

At this point we’ve translated the model instance into Python native datatypes. To finalize the serialization process we render the data into Json.

 

 

 

Thank you for visiting my website! If you enjoyed the free courses, please consider supporting my works on Patreon.
Exercise Files
No Attachment Found
No Attachment Found