

{"id":71856,"date":"2019-11-18T16:47:20","date_gmt":"2019-11-18T11:17:20","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=71856"},"modified":"2021-06-21T12:19:12","modified_gmt":"2021-06-21T06:49:12","slug":"django-rest-framework","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/django-rest-framework\/","title":{"rendered":"Django REST Framework Tutorial &#8211; Feel Blessed!! &#8216;Coz Boss wants you to REST"},"content":{"rendered":"<p>We will learn to build a REST API in Django through a very simple method. Just follow the below steps and your first API will be ready to get going with minimum code.<\/p>\n<p>Django REST Framework (DRF) allows developers to rapidly build RESTful APIs. We will see how DRF is actually used and also clear some very basics of the web. This article assumes though, that you have a basic understanding of the working of the web.<\/p>\n<p>We will learn topics like Django for APIs, RESTful web services and then build up to DRF.<\/p>\n<p>Okay, so buckle up for this article.<\/p>\n<h3>What is an API?<\/h3>\n<p>Before understanding what DRF can do we first need to understand what is an API. API is an acronym for <strong>Application Programming Interface<\/strong>. Two machines use it to communicate with each other. We will be dealing with Web APIs and then the definition changes to:<\/p>\n<p><em>An API is used by two applications trying to communicate with each other over a network or Internet. <\/em><em>With this definition in mind, let\u2019s look at a block diagram.<\/em><\/p>\n<p style=\"text-align: center\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/Working-example-of-REST-API-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-72984 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/Working-example-of-REST-API-1.png\" alt=\"Working example of REST API - Django REST Framework\" width=\"790\" height=\"390\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/Working-example-of-REST-API-1.png 790w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/Working-example-of-REST-API-1-150x74.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/Working-example-of-REST-API-1-300x148.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/Working-example-of-REST-API-1-768x379.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/Working-example-of-REST-API-1-520x257.png 520w\" sizes=\"auto, (max-width: 790px) 100vw, 790px\" \/><\/a><\/p>\n<p>The above figure helps in visualizing the working of an API. The API acts as a mediator between Django and other applications. As you can see, other applications can be from Android, iOS, Web apps, browsers, etc.<\/p>\n<p>The API&#8217;s main task is to receive data from other applications and provide them to the backend. This data is usually in JSON format.<\/p>\n<p>Okay, so some of you may ask what about those so-called Google APIs. Okay, let\u2019s see. They are also APIs of the same nature. Understand it like Google providing the API as a service.<\/p>\n<p>Here, you can see some of the common examples of APIs used by developers.<\/p>\n<p><a href=\"https:\/\/developers.google.com\/maps\/documentation\/javascript\/tutorial\">API Examples<\/a><\/p>\n<p style=\"text-align: center\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Google-Maps-API.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-71969\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Google-Maps-API.png\" alt=\"Google Maps API - Django REST Framework\" width=\"1920\" height=\"943\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Google-Maps-API.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Google-Maps-API-150x74.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Google-Maps-API-300x147.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Google-Maps-API-768x377.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Google-Maps-API-1024x503.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Google-Maps-API-520x255.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>This is the link of Google geocoding API used to insert the functionality of Google Maps on your website. This is one of the many examples. Not just Google but many companies provide their APIs as a service. These services are used by the developers to provide maps service to end-users.<\/p>\n<p>There is nothing different for these APIs and the ones we discussed earlier. These APIs do the same thing, they simply transfer data from our application to the Google server. The server then returns the response through this very same API. That response is then passed by our server to the end-user.<\/p>\n<p>Sometimes, this can be bypassed to directly give a response to the end-user.<\/p>\n<p><strong>APIs are programs that exist to transfer data between applications. They are responsible for cleaning and formatting data correctly.<\/strong><\/p>\n<h3>What are RESTful APIs?<\/h3>\n<p>Well, first we need to understand what is REST. Don\u2019t worry, I will explain it in simple words.<\/p>\n<p>REST stands for <strong>Representational State Transfer<\/strong>. REST is an architecture on which we develop web services. Web services can be understood as your device connects to the internet.<\/p>\n<p>When you search for anything on Google or watch something on YouTube, these are web services where your device is communicating to a server.<\/p>\n<p>When these web services use REST Architecture, they are called RESTful Web Services. These web services use HTTP to transmit data between machines. now, back to the question, what are RESTful APIs?<\/p>\n<p><strong>A RESTful API acts as a translator between two machines communicating over a Web service.<\/strong> This is just like an API but it&#8217;s working on a RESTful Web service. Web developers program REST API such that server can receive data from applications.<\/p>\n<p>These applications can be web-apps, Android\/iOS apps, etc. RESTful APIs today return JSON files that can be interpreted by a variety of devices.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/django-rest-API-applications.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-72980\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/django-rest-API-applications.png\" alt=\"django rest API applications - Django REST Framework\" width=\"653\" height=\"426\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/django-rest-API-applications.png 653w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/django-rest-API-applications-150x98.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/django-rest-API-applications-300x196.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/django-rest-API-applications-520x339.png 520w\" sizes=\"auto, (max-width: 653px) 100vw, 653px\" \/><\/a><\/p>\n<p>The RESTful API will provide standardized data to all devices. Currently, that is achieved by JSON format but REST is not limited to that. Okay, for the last part JSON, we will see it in further sections.<\/p>\n<p>One last question to be answered.<\/p>\n<h3>What is Django REST Framework?<\/h3>\n<p>So, as we learned in previous sections, DRF is an acronym for Django REST Framework. (stating the obvious) It\u2019s used to develop REST APIs for Django. Yup, DRF is used to develop RESTful APIs which is both easy and a smart way.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/django-rest-framework-2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-72982\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/django-rest-framework-2.jpg\" alt=\"django rest framework tutorial\" width=\"802\" height=\"420\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/django-rest-framework-2.jpg 802w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/django-rest-framework-2-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/django-rest-framework-2-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/django-rest-framework-2-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/django-rest-framework-2-520x272.jpg 520w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/a><\/p>\n<p>DRF is a framework built upon the Django Framework. It is not a separate framework. You can say that it is <em>a tool which alongside Django is used to develop RESTful APIs<\/em>. It increases the development speed. It also addresses various security issues natively.<\/p>\n<p>DRF is an Open Source Software, just like Django. It is actively developed by various MNCs. If you made any profit from this framework then please donate it to keep it open source.<\/p>\n<p>The documentation of DRF is actually considered better than Django\u2019s. The only catch is that you need to have a bit of previous knowledge to understand the DRF documentation. Django is, of course, the prerequisite but also some basic knowledge of REST is necessary.<\/p>\n<p>Also, we will discuss various features of the Django Rest Framework alongside.<\/p>\n<p><strong>Note:\u00a0<\/strong>All the code for this article is in this repository:<\/p>\n<p><a href=\"https:\/\/drive.google.com\/file\/d\/1bRQjAWBRsNorvN988pee80YyClUDEMQt\/view\">Django Rest Framework Tutorial<\/a><\/p>\n<h3>Making a basic_api with DRF<\/h3>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/django-rest-framework1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-72985 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/django-rest-framework1.jpg\" alt=\"working of django REST framework\" width=\"802\" height=\"420\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/django-rest-framework1.jpg 802w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/django-rest-framework1-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/django-rest-framework1-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/django-rest-framework1-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/django-rest-framework1-520x272.jpg 520w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/a><\/p>\n<p>Let\u2019s start by making a new django-project. In your virtual environment, make a new project <strong>DRFtutorial<\/strong>. We will develop a simple API to understand what is done by DRF.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\"> django-admin startproject DRFtutorial<\/pre>\n<p>When you make the project just move inside the DRFtutorial directory. Then run<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\"> python manage.py startapp basic_api<\/pre>\n<p style=\"text-align: center\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Starting-Django-project.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-71973\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Starting-Django-project.png\" alt=\"Starting Django project - django drf\" width=\"1920\" height=\"972\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Starting-Django-project.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Starting-Django-project-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Starting-Django-project-300x152.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Starting-Django-project-768x389.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Starting-Django-project-1024x518.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Starting-Django-project-520x263.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>The basic_api is created. Now, we also need to install Django REST Framework. To install it, execute this command.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\"> pip install djangorestframework==3.10<\/pre>\n<p style=\"text-align: center\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Installing-Django-REST-Framework.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-71974\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Installing-Django-REST-Framework.png\" alt=\"Installing Django REST Framework\" width=\"1920\" height=\"972\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Installing-Django-REST-Framework.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Installing-Django-REST-Framework-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Installing-Django-REST-Framework-300x152.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Installing-Django-REST-Framework-768x389.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Installing-Django-REST-Framework-1024x518.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Installing-Django-REST-Framework-520x263.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>Now, open <strong>settings.py<\/strong> file of your project. There install these two apps, <strong>\u2018rest_framework\u2019<\/strong> &amp; <strong>\u2018basic_api\u2019<\/strong>. Just include them in the INSTALLED_APPS list.<\/p>\n<p style=\"text-align: center\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Installing-Apps-in-Project.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-71975\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Installing-Apps-in-Project.png\" alt=\"Installing Apps in Project - Django REST Framework \" width=\"1920\" height=\"976\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Installing-Apps-in-Project.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Installing-Apps-in-Project-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Installing-Apps-in-Project-300x153.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Installing-Apps-in-Project-768x390.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Installing-Apps-in-Project-1024x521.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Installing-Apps-in-Project-520x264.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>Now, paste this code of model in <strong>basic_api\/models.py<\/strong> file.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\"># basic_api\/models.py\r\nfrom django.db import models\r\n\r\n#list\r\nGrade = [\r\n    ('excellent', 1),\r\n    ('average', 0),\r\n    ('bad', -1)\r\n]\r\n\r\n# DataFlair\r\nclass DRFPost(models.Model):\r\n    name = models.CharField(max_length = 100)\r\n    author = models.CharField(max_length = 100)\r\n    uploaded = models.DateTimeField(auto_now_add = True)\r\n    rating = models.CharField(choices = Grade, default = 'average', max_length = 50)\r\n\r\n    class Meta:\r\n        ordering = ['uploaded']\r\n\r\n    def __str__(self):\r\n        return self.name\r\n<\/pre>\n<p style=\"text-align: center\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Basic-api-models-py.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-71976\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Basic-api-models-py.png\" alt=\"Basic api\/models.py - Django REST Framework\" width=\"1920\" height=\"976\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Basic-api-models-py.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Basic-api-models-py-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Basic-api-models-py-300x153.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Basic-api-models-py-768x390.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Basic-api-models-py-1024x521.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Basic-api-models-py-520x264.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>Also, register this model with django-admin. Add this code to <strong>basic_api\/admin.py<\/strong> file.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">from django.contrib import admin\r\nfrom basic_api.models import DRFPost\r\n\r\n# DataFlair\r\nadmin.site.register(DRFPost)<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-admin-py.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-71977\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-admin-py.png\" alt=\"basic_api-admin.py - django rest\" width=\"1920\" height=\"976\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-admin-py.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-admin-py-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-admin-py-300x153.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-admin-py-768x390.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-admin-py-1024x521.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-admin-py-520x264.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>Ok, now we need to migrate our changes in the database. Execute these two commands in the terminal.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\u00a0python manage.py makemigrations\r\n\u00a0python manage.py migrate<\/pre>\n<p style=\"text-align: center\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Performing-migrations-to-Database.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-71978\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Performing-migrations-to-Database.png\" alt=\"Performing migrations to Database - Django REST Framework\" width=\"1920\" height=\"972\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Performing-migrations-to-Database.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Performing-migrations-to-Database-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Performing-migrations-to-Database-300x152.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Performing-migrations-to-Database-768x389.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Performing-migrations-to-Database-1024x518.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Performing-migrations-to-Database-520x263.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>Now, we can use the admin to populate our database. Also, before that please create a superuser. You can create a superuser by executing the following command.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\u00a0python manage.py createsuperuser<\/pre>\n<p>Fill in your details which prompts you and then you should get something similar to this image.<\/p>\n<p style=\"text-align: center\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/createsuperuser-command-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-71979\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/createsuperuser-command-1.png\" alt=\"createsuperuser command - django drf\" width=\"949\" height=\"359\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/createsuperuser-command-1.png 949w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/createsuperuser-command-1-150x57.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/createsuperuser-command-1-300x113.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/createsuperuser-command-1-768x291.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/createsuperuser-command-1-520x197.png 520w\" sizes=\"auto, (max-width: 949px) 100vw, 949px\" \/><\/a><\/p>\n<p>Start your server and populate your database with the use of Django-admin.<\/p>\n<p><strong>Execute command:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\u00a0python manage.py runserver<\/pre>\n<p>Then open URL: <em>localhost:8000\/admin<\/em><\/p>\n<p>After that, log in with your superuser credentials. Then add some records in the <strong>basic_api<\/strong> app.<\/p>\n<p style=\"text-align: center\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Populating-database.png\"><img loading=\"lazy\" decoding=\"async\" class=\"img-gray-border aligncenter wp-image-71980 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Populating-database.png\" alt=\"Populating database - Django REST Framework\" width=\"1920\" height=\"943\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Populating-database.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Populating-database-150x74.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Populating-database-300x147.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Populating-database-768x377.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Populating-database-1024x503.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/Populating-database-520x255.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>After that, our first part of DRF comes into play.<\/p>\n<h3>Serializers<\/h3>\n<p>We developed our models in the previous section. In this section, we will develop serializers. Let\u2019s understand what are these serializers used for.<\/p>\n<p><strong>Serializers are used to transform our model instances or objects into JSON.<\/strong><\/p>\n<p>An API\u2019s end result is always JSON. API\u2019s communicate with multiple technologies which take JSON as their Input. JSON stands for JavaScript Object Notation. It is simply a JavaScript notation.<\/p>\n<p>You can say it&#8217;s similar to the <em><strong>Python dictionary<\/strong><\/em>. It&#8217;s just a file that is sent\/ received by the API to various applications. That\u2019s what serializers help us do here.<\/p>\n<p>Serializers are an important part of DRF. We will be using serializers to get the JSON files for our models. It\u2019s not easy to implement with just Django. Serializers can easily create JSON responses from our model instances. This is one of the magical things DRF can perform.<\/p>\n<p>To implement <strong>serializers<\/strong> in our project, create a new file in the <strong>basic_api<\/strong> directory called <strong>serializers.py<\/strong>. Then paste this code in the file <strong>basic_api\/serializers.py<\/strong>:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">from rest_framework import serializers\r\nfrom basic_api.models import DRFPost\r\n\r\nclass DRFPostSerializer(serializers.ModelSerializer):\r\n    class Meta:\r\n        model = DRFPost\r\n        fields = '__all__'<\/pre>\n<p style=\"text-align: center\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-serializers-py.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-71982\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-serializers-py.png\" alt=\"basic_api-serializers-py - Django REST Framework\" width=\"1920\" height=\"976\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-serializers-py.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-serializers-py-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-serializers-py-300x153.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-serializers-py-768x390.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-serializers-py-1024x521.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-serializers-py-520x264.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p><strong>Understand the code:<\/strong><\/p>\n<p>Let\u2019s understand what we just did. We created a new file <strong>serializers.py<\/strong> in our application. This file contains the code similar to <strong>modelforms<\/strong> code.<\/p>\n<p>We imported the <strong>serializer<\/strong> class from the <strong>rest_framework<\/strong> module. This is the DRF used in Django. They couldn\u2019t use Django as it would conflict with the existing name itself. Then we imported the model we wanted serializer for.<\/p>\n<p>Then like<strong> model forms class<\/strong>, we created <strong>serializer class<\/strong>. This is called the<strong> model serializer<\/strong> class. The model serializer class will take the model attribute and field attribute.<\/p>\n<p>The model attribute will input the model for the serializer. The fields attribute takes a list containing the names of fields. These are the fields which will be passed as a json file. Now, we will also need views and urls to see how all this works.<\/p>\n<h3>Adding views<\/h3>\n<p>We are going to use class_based views here. This is different from function-based views. In <strong>basic_api\/views.py<\/strong> file, paste this code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">from django.shortcuts import render\r\nfrom rest_framework import generics\r\nfrom basic_api.models import DRFPost\r\nfrom basic_api.serializers import DRFPostSerializer\r\n\r\nclass API_objects(generics.ListCreateAPIView):\r\n    queryset = DRFPost.objects.all()\r\n    serializer_class = DRFPostSerializer\r\n\r\nclass API_objects_details(generics.RetrieveUpdateDestroyAPIView):\r\n    queryset = DRFPost.objects.all()\r\n    serializer_class = DRFPostSerializer<\/pre>\n<p style=\"text-align: center\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-views-py.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-71983\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-views-py.png\" alt=\"basic_api-views-py - django rest\" width=\"1920\" height=\"976\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-views-py.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-views-py-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-views-py-300x153.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-views-py-768x390.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-views-py-1024x521.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-views-py-520x264.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p><strong>Understand the code:<\/strong><\/p>\n<p>The views written here are simply CBVs or class-based views. Also, they are not simple CBVs but DRFs generic class\u2019s CBVs.<\/p>\n<p>In the first 4 lines, we are importing all the important things we are going to need in view. We imported <strong>models, serializer class and generics class<\/strong>. The generics class will provide us with the web browsable API. The generics class is a special class in rest_framework.<\/p>\n<p>There are also generic views for Django itself. They are for a different purpose.<\/p>\n<p>You need to keep it in mind that this is an API. It&#8217;s not meant to be displayed to the user. It&#8217;s for programs or applications communication. The generics class will provide us with important classes. These classes make it easy to deal with RESTful requests and send JSON data.<\/p>\n<p>The first view class is <strong>Post_objects<\/strong>. This class extends the class <strong>generics.ListCreateAPIView class<\/strong>. It is clear from the name. This class will be used to provide a list of all model instances. These model instances will be converted to <em><strong>JavaScript objects<\/strong> <\/em>and transmit to application.<\/p>\n<p>There are two fields in this class. The first is the Queryset attribute. This attribute will provide the model instances to be operated on. The view classes are written by DRF developers and we need not learn what\u2019s it doing behind the scenes. The second attribute it asks for is <strong>serializer_class<\/strong>.<\/p>\n<p>The serializer class we just imported is entered there.<\/p>\n<p>This means that this is the serializer model to be used to create a JSON model. The view part will handle the calling of correct classes and queries. It will then respond with the JSON response to the client or other application.<\/p>\n<p>The second view class is also similar to the 1st. I discussed earlier; this view logics are not needed to be known. You should know what results we get. You do not need to reinvent the wheel or have an understanding of the whole framework. The second class will provide details of an individual object.<\/p>\n<p>There are more operations that can be performed on an individual object. Therefore, we are using class <strong>generics.RetrieveUpdateDestroyAPIView class<\/strong>. This class will provide Read, Update and Delete Functions on the model instance. Ok, now let\u2019s set up the urls.<\/p>\n<h4>Configuring URLs<\/h4>\n<p>Now, create a new file as <strong>basic_api\/urls.py<\/strong>. This is urls for the <strong>basic_api<\/strong> app. Paste this code in the basic_api\/urls.py file.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\"># basic_api\/urls.py\r\nfrom django.urls import path\r\nfrom rest_framework.urlpatterns import format_suffix_patterns\r\nfrom basic_api import views\r\n\r\nurlpatterns = [\r\n    path('basic\/', views.API_objects.as_view()),\r\n    path('basic\/&lt;int:pk&gt;\/', views.API_objects_details.as_view()),\r\n]\r\n\r\nurlpatterns = format_suffix_patterns(urlpatterns)\r\n\r\n<\/pre>\n<p style=\"text-align: center\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-urls-py.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-71984\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-urls-py.png\" alt=\"basic_api\/urls.py - Django REST Framework\" width=\"1920\" height=\"976\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-urls-py.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-urls-py-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-urls-py-300x153.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-urls-py-768x390.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-urls-py-1024x521.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/basic_api-urls-py-520x264.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p><strong>Understanding the code:<\/strong><\/p>\n<p>In this code, we have created a urls.py file. There are some new terms here though. The new import is from <strong>rest_framework.urlpatterns<\/strong>. The <strong>format_suffix_patterns<\/strong> method will be used to generalize our urls. It will make the urls to work with URIs like<\/p>\n<p>http:\/\/basic\/4 and http:\/\/basic\/4.json<\/p>\n<p>Both of these urls can be handled by our web app. Then we imported the views from the basic_api app. After that, we simply defined the urls.<\/p>\n<p>The new part is the view function we have written. This is what you call a view from a Class-Based Views in Django. In the view functions, we write the name of the class. The class name is always accompanied by the <strong>as_view()<\/strong> method. This is true for all CBVs out there and is not limited to DRF.<\/p>\n<p>That\u2019s it. After that the<strong> format_sufix_patterns()<\/strong> is used on urlpatterns.<\/p>\n<p><strong>Configuring main urls.py file<\/strong><\/p>\n<p>Paste these lines of code in the <strong>DRFtutorial\/urls.py<\/strong> file.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">from django.contrib import admin\r\nfrom django.urls import path, include\r\n\r\nurlpatterns = [\r\n    path('admin\/', admin.site.urls),\r\n    path('', include('basic_api.urls'))\r\n]\r\n<\/pre>\n<p style=\"text-align: center\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/DRFtutorials-urls-py.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-71985\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/DRFtutorials-urls-py.png\" alt=\"DRFtutorials\/urls.py - django drf\" width=\"1920\" height=\"976\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/DRFtutorials-urls-py.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/DRFtutorials-urls-py-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/DRFtutorials-urls-py-300x153.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/DRFtutorials-urls-py-768x390.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/DRFtutorials-urls-py-1024x521.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/DRFtutorials-urls-py-520x264.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>The setup is now complete. Save your changes and again reload the server. After you have reloaded the server, open this URL:\u00a0<em>http:\/\/localhost:8000\/basic\/<\/em><\/p>\n<p>It will come up with this output.<\/p>\n<p style=\"text-align: center\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/DRFtutorials-output.png\"><img loading=\"lazy\" decoding=\"async\" class=\"img-gray-border aligncenter wp-image-71986 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/DRFtutorials-output.png\" alt=\"DRFtutorials output\" width=\"1920\" height=\"943\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/DRFtutorials-output.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/DRFtutorials-output-150x74.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/DRFtutorials-output-300x147.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/DRFtutorials-output-768x377.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/DRFtutorials-output-1024x503.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/DRFtutorials-output-520x255.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>From the urls.py file, you can easily say that this is the list of objects.<\/p>\n<p>The code you can see in <strong>square braces []<\/strong> is the actual JSON output. I told you earlier, JSON looks very similar to <em><strong>Dictionaries in Python<\/strong><\/em>.<\/p>\n<h3>JSON<\/h3>\n<p>For a brief introduction, JSON is simply a JavaScript Data Structure. It stores values in a key: value pairs. JSON is simply a format which is used as a standard for data interchange. It is lightweight and therefore, takes in less bandwidth.<\/p>\n<p>A simple JSON object from our example is:<\/p>\n<p style=\"text-align: center\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/JSON-object.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-71987\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/JSON-object.png\" alt=\"JSON object - Django REST Framework\" width=\"454\" height=\"170\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/JSON-object.png 454w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/JSON-object-150x56.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/JSON-object-300x112.png 300w\" sizes=\"auto, (max-width: 454px) 100vw, 454px\" \/><\/a><\/p>\n<p>Here the <strong>keys<\/strong> are strings and <strong>values<\/strong> can be strings or other data-structures. The values can be objects too. JSON is beginner-friendly and easy to use.<\/p>\n<p>The <strong>id<\/strong> is key and<strong> 1<\/strong> is <strong>value<\/strong>. That is the formatted data that applications can use to extract information. Structured information is always favored.<\/p>\n<p>Now, your search for this URL:\u00a0\u00a0<em>http:\/\/localhost:8000\/basic\/2\/<\/em><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/DRFtutorials-output-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"img-gray-border aligncenter wp-image-71988 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/DRFtutorials-output-2.png\" alt=\"DRFtutorials output 2\" width=\"1920\" height=\"943\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/DRFtutorials-output-2.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/DRFtutorials-output-2-150x74.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/DRFtutorials-output-2-300x147.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/DRFtutorials-output-2-768x377.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/DRFtutorials-output-2-1024x503.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/DRFtutorials-output-2-520x255.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>This is the output rendered by the browser. The 2 in the URL is the id of the object. It is a specialty of Django to automatically attach id attribute to models. Notice, now we are getting only one object. That object has id = 2.<\/p>\n<p>You can see, the first output was a list of objects. The second output is a single object. Also, at the bottom of the page, there is an HTML form. This form is used to modify the current object. If you modify and click on the PUT button, then the object information will change.<\/p>\n<p>You can also delete objects by clicking on the delete button.<\/p>\n<h3>What\u2019s DRF doing here?<\/h3>\n<p>Many would ask at this time that what\u2019s the role of DRF. All this could have been achieved by Django Admin if I wanted to perform <em><strong>CRUD operations<\/strong><\/em>. Well, it is true. The point is DRF is for applications. The DRF has this special feature of browsable API.<\/p>\n<p>Otherwise, you would have seen just the JSON response like this.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/JSON-response.png\"><img loading=\"lazy\" decoding=\"async\" class=\"img-gray-border aligncenter wp-image-71989 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/JSON-response.png\" alt=\"JSON response - django drf\" width=\"1920\" height=\"1030\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/JSON-response.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/JSON-response-150x80.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/JSON-response-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/JSON-response-768x412.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/JSON-response-1024x549.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/10\/JSON-response-520x279.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>DRF makes it easier for a developer to debug their APIs. The other big feature of DRF is that it converts the Models into serializers. What we have done in this Django REST Framework tutorial is simply made classes and extended them with built-in classes.<\/p>\n<p>If we needed to have this kind of architecture, we would need so much more code in place.<\/p>\n<p>DRF made it so easy for us to serialize data.<\/p>\n<p>The serializers not only take our models and transmit them as JSON objects. They also provide data from users to the backend. They automatically clean the data or validate data. This is a plus from DRF. Cleaned data removes all the security issues.<\/p>\n<p>Believe me, the security provided by DRF is well established. Django REST Framework has many more features than explained here. They go well beyond the scope of just Django. You will need a better understanding of APIs and REST architecture. It is not so difficult but will take time.<\/p>\n<h3>Summary<\/h3>\n<p>We can say that DRF is an amazing extension to Django. Django is capable on its own but DRF takes that one step ahead. Django\u2019s rapid development is boosted by the use of DRF. Django Rest Framework has more features like Requests and response methods.<\/p>\n<p>There is so much DRF is capable of and the same goes for all readers. DRF will let you communicate with various applications over the internet. The most prominent Django REST Framework examples are of Android Applications and Web apps. You will only have different UI\/ Ux. The backend will be Django.<\/p>\n<p>API communication is handled by Django REST Framework. That\u2019s how you can do full-stack development.<\/p>\n<p><em>Freely mention your queries regarding the DRF tutorial in the comment section.<\/em><span hidden class=\"__iawmlf-post-loop-links\" data-iawmlf-links=\"[{&quot;id&quot;:1327,&quot;href&quot;:&quot;https:\\\/\\\/developers.google.com\\\/maps\\\/documentation\\\/javascript\\\/tutorial&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20200715194436\\\/https:\\\/\\\/developers.google.com\\\/maps\\\/documentation\\\/javascript\\\/tutorial&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-09 04:49:17&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2025-12-15 02:43:16&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2025-12-21 17:05:07&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2025-12-26 07:30:57&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2025-12-30 16:16:24&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-01-05 04:34:51&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-01-09 04:46:51&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-01-14 09:32:13&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-01-21 19:21:19&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-01-25 23:35:08&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-01-29 16:16:52&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-02-03 05:12:00&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-02-06 19:29:51&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-02-11 08:23:56&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-02-14 11:27:08&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-02-18 04:11:27&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-02-21 19:18:51&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-02-25 07:00:26&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-03-02 16:37:33&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-03-07 05:46:01&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-03-11 11:20:04&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-03-15 01:36:41&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-03-18 09:15:57&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-03-22 16:49:54&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-03-26 21:52:46&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-04-01 15:44:29&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-04-05 17:35:27&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-04-10 09:48:15&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-04-14 05:01:48&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-04-17 07:45:32&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-04-20 15:18:49&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-04-23 21:51:22&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-04-27 11:42:35&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-04-30 20:21:28&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-05-04 09:26:55&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-05-07 10:37:00&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-05-11 09:17:48&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-05-15 14:38:36&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-05-21 04:02:14&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-05-28 12:00:52&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-06-01 09:42:45&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-06-05 13:32:33&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-06-09 03:59:49&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-06-12 12:17:06&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-06-16 21:18:18&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-06-21 11:40:08&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-06-25 09:56:45&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-06-28 13:54:17&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-07-02 03:41:58&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-07-06 06:52:16&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-07-09 14:19:22&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-07-12 23:51:15&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-07-16 14:47:24&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-07-20 07:59:26&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-07-24 04:51:23&quot;,&quot;http_code&quot;:404}],&quot;broken&quot;:true,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-07-24 04:51:23&quot;,&quot;http_code&quot;:404},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:1328,&quot;href&quot;:&quot;https:\\\/\\\/drive.google.com\\\/file\\\/d\\\/1bRQjAWBRsNorvN988pee80YyClUDEMQt\\\/view&quot;,&quot;archived_href&quot;:&quot;&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[],&quot;broken&quot;:false,&quot;last_checked&quot;:null,&quot;process&quot;:&quot;done&quot;}]\"><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We will learn to build a REST API in Django through a very simple method. Just follow the below steps and your first API will be ready to get going with minimum code. Django&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":72982,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19149],"tags":[19568,21379,21378,21380,21377],"class_list":["post-71856","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-django","tag-django-rest-framework","tag-django-rest-framework-example","tag-django-rest-framework-logging","tag-install-django-rest-framework","tag-python-rest-api-django"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Django REST Framework Tutorial - Feel Blessed!! &#039;Coz Boss wants you to REST - DataFlair<\/title>\n<meta name=\"description\" content=\"Django REST Framework - Learn the core features of DRF along with a brief explanation of API, RESTful APIs &amp; JSON. The DRF tutorial includes source code &amp; screenshots for better understanding.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/django-rest-framework\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Django REST Framework Tutorial - Feel Blessed!! &#039;Coz Boss wants you to REST - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Django REST Framework - Learn the core features of DRF along with a brief explanation of API, RESTful APIs &amp; JSON. The DRF tutorial includes source code &amp; screenshots for better understanding.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/django-rest-framework\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-11-18T11:17:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-06-21T06:49:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/django-rest-framework-2.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"17 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Django REST Framework Tutorial - Feel Blessed!! 'Coz Boss wants you to REST - DataFlair","description":"Django REST Framework - Learn the core features of DRF along with a brief explanation of API, RESTful APIs & JSON. The DRF tutorial includes source code & screenshots for better understanding.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/django-rest-framework\/","og_locale":"en_US","og_type":"article","og_title":"Django REST Framework Tutorial - Feel Blessed!! 'Coz Boss wants you to REST - DataFlair","og_description":"Django REST Framework - Learn the core features of DRF along with a brief explanation of API, RESTful APIs & JSON. The DRF tutorial includes source code & screenshots for better understanding.","og_url":"https:\/\/data-flair.training\/blogs\/django-rest-framework\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2019-11-18T11:17:20+00:00","article_modified_time":"2021-06-21T06:49:12+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/django-rest-framework-2.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"17 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/django-rest-framework\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/django-rest-framework\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"Django REST Framework Tutorial &#8211; Feel Blessed!! &#8216;Coz Boss wants you to REST","datePublished":"2019-11-18T11:17:20+00:00","dateModified":"2021-06-21T06:49:12+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/django-rest-framework\/"},"wordCount":2674,"commentCount":3,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/django-rest-framework\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/django-rest-framework-2.jpg","keywords":["Django Rest Framework","django rest framework example","django rest framework logging","install django rest framework","python rest api django"],"articleSection":["Django Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/django-rest-framework\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/django-rest-framework\/","url":"https:\/\/data-flair.training\/blogs\/django-rest-framework\/","name":"Django REST Framework Tutorial - Feel Blessed!! 'Coz Boss wants you to REST - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/django-rest-framework\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/django-rest-framework\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/django-rest-framework-2.jpg","datePublished":"2019-11-18T11:17:20+00:00","dateModified":"2021-06-21T06:49:12+00:00","description":"Django REST Framework - Learn the core features of DRF along with a brief explanation of API, RESTful APIs & JSON. The DRF tutorial includes source code & screenshots for better understanding.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/django-rest-framework\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/django-rest-framework\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/django-rest-framework\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/django-rest-framework-2.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/11\/django-rest-framework-2.jpg","width":802,"height":420,"caption":"django rest framework tutorial"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/django-rest-framework\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Django Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/django\/"},{"@type":"ListItem","position":3,"name":"Django REST Framework Tutorial &#8211; Feel Blessed!! &#8216;Coz Boss wants you to REST"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/71856","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=71856"}],"version-history":[{"count":19,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/71856\/revisions"}],"predecessor-version":[{"id":97660,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/71856\/revisions\/97660"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/72982"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=71856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=71856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=71856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}