Web Development in Python: Django Rest Framework: Serialization, Requests, and Response
Add Django Rest Framework

Install Django Rest Framework:

pip install djangorestframework

Add then add rest_framework in INSTALLED_APPS array:

INSTALLED_APPS = [ ... 'home', 'rest_framework' ]

Add Models

Run the following command to make the migrations:

python3 manage.py migrate

python3 manage.py makemigrations

You can learn more about models here.

Serialization

In DRF, serializers are responsible for converting complex data such as querysets and model instances to native Python datatypes ( called serialization ) that can be easily rendered into JSON, XML, or other content types which can be rendered by front-end.

Serializers are also responsible for deserialization which means it allows parsed data to be converted back into complex types, after first validating the incoming data.

Serializer Class

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

We have our database with 3 fields ( topic, author, body ). However, we need to make some adjustment in order for us to take data from the user, store in the database, convert it into JSON, and send it back to front-end. In technical terms, the complex data type (in form of model instance ) first converted into Python Native Data Type, and later this convert to JSON data.

Add serializers.py

line 1: we are importing the serializers module from rest_framework.

line 2: we are creating a class name PostSerializers, which is inheriting from the Serializer class inside of the serializers module.

line 3,4,5: if you ever made forms in Django, you would see this is very similar to forms. We are creating the instances of the same fields as in our models.py file.

Add views.py

line 1: Importing the render method.

line 2: importing Post model from models.py file

line 3: importing PostSerializers

line 4: Importing JSONRender so we can convert the data to JSON type.

line 5: importing the HttpResponse just to display the output.

line 8: creating a functional view, which takes request as an argument.

line 9: new_post variable stores the object with id = 1 only.

line 10: we are serializing the new_post variable and storing that object in the serializer variable.

line 11: json_data variable storing the data as JSON, the JSONRenderer renders the request data into JSON.

line 12: it returns the value of the function as an HttpResponse in the form of JSON

we can add a few lines in our detailed function (post_detail.py) to show what is going on behind the scene.

you can check in your terminal, you will see some details of what these variables (new_post, serializer,json_data ) prints. It will be fun!!

Add urls.py

just add the endpoint for views we have created in the ecom/ursl.py file.

Add data to your table by going to http://127.0.0.1:8000/admin/home/post/ and click on the ADD POST button.
['Saranjeet Singh']
['Coding', 'Programming', 'Python', 'Web Development', 'Technology']