Introduction to Django REST Framework
Do you consider yourself a total newbie to web development? Are you bored with everyone around you using the term DRF all the time without even understanding what it is? Then, this post is just right for you. You don’t have to know how to code or anything about web development. You will start from the basics and gradually move on to more complex things.
What is API?
Consider the food-ordering applications like Zomato or Swiggy that we all use on a daily basis. Have you ever thought where these applications get details about restaurants, menus, and pricing from? Well, they do not store them in the application but somewhere else in some distant data centres, maybe thousands of kilometres away from you.
There must be a mechanism through which the communication from your mobile app can reach the server. The name given to this mechanism is API(Application Programming Interface). API is the messenger through which information goes from your app to the server and from the server to your mobile application.
To illustrate how API works, consider the following scenario. Suppose that your mobile application sends a request to the server, which says "show me the menu of this restaurant." In response to this request, the server sends you the data in JSON format, such as:
This means that any time you open an app and view fresh information appearing on your phone screen, an API request has just been made.
What Is the Role of DRF Then?
However, now comes the most interesting part. The Django framework, by default, is constructed in a manner to create HTML pages automatically. The buttons, forms, and all the rest of the content that you observe in your browser when working with Django applications are usually created using the standard methods provided by the framework.
But in many cases, HTML pages may not be necessary at all to ensure the proper functioning of the application. For example, the React web application, Flutter mobile app, or any other connected piece of software does not need to have an HTML page, but it only needs to receive JSON data.
It is precisely the niche that is filled by the Django REST Framework (DRF). The DRF is an additional module that is built on top of Django. It allows transforming your data into JSON and delivering it via API instead of creating an HTML page.
Example: Let’s take an example to understand this concept clearly. Suppose you have a database with information about students, which includes name, age, and courses the students are studying. In the case of DRF, all you need to do is first serialise the information using something known as a serializer and convert it into JSON format. Then, you will expose the JSON data at a certain endpoint on your site, let us say, yoursite.com/api/students. Lastly, you will define who will have access to this endpoint; everyone can have access or only administrators, or it can be limited to only logged-in users.
Why do Developers Love DRF?
There are multiple reasons why developers always prefer to use DRF while working with Django APIs:
How to Get Started
Starting to work with DRF is relatively easy when you have an understanding of Django basics. First, create a Django project, after which use pip to install DRF, then create serializers for your models. Create your views based on the serializers you just created, and lastly, connect everything using DRF’s routers. Thanks to the direct relationship with Django concepts you probably know already, learning DRF does not mean that you need to learn a whole new thing.
Conclusion
Essentially, DRF is the link between your understanding of Django and the creation of full-fledged APIs


