What is Django Rest Framework and why you should learn it

If you think Django does “magic” because you can do so much with just a few lines, wait until you discover what Django Rest Framework is.


What is an API?
Why use an API?
Why Django Rest Framework?
How to learn Django Rest Framework
Conclusion

What is an API?

Django Rest Framework lets you create RESTful APIs: A way to transfer information between an interface and a database in a simple way.

It separates user interface and data storage and communicates user and database sending a .json file. Just like this one:

Django Rest Framework view
DRF in-built visual interface

Think about the last time you were shopping online: Do you remember that each article had the same structure but had different information?

They created the structure using HTML and CSS, and then used an API to populate each article.


Why use an API?

You can’t understand what an API is unless you understand what it can do.

Imagine that you want to create a web application. Obviously, you would choose Django.

You create it and it’s a hit! Everybody loves your web application!

They love it so much, that they want a mobile phone version!

Time to create an Android Version! And an iOs version.

And then you have to replicate 2 times the same code to Add, View, Remove, Update and Delete data, with different languages.

That opens the door to many possible mistakes…

But despite that, let’s say you managed and created the same application, using 3 different languages.

Now you notice that your website is static. You want your web app to be more dynamic and modern. You need Javascript for that.

While you can do it with just Vanilla Javascript, it is better to use a JS framework as React or Vue.

Now you need to re-do what you did in Django, iOs and Android again.

Or you can create a REST API.

With DRF, you can create an API to serve information to the user’s interface.

You can create a FrontEnd with Vue, an Android and iOs application, and all three different platforms are connected to the same API

API REST Structure

You only need to create the logic once, and each of your platforms connect to the same API.

This way, if you want to update it or add more functionality, you only need to change in one side, not on each platform.

It is easier this way, right? Easier, more scalable and more reliable.

DRF takes the heavyweight of the database interaction and logic to just serve you the information. No matter how many platforms you use or the language they are built. As simple as that.


Why Django Rest Framework?

Django rest framework logo

We know now that REST APIs are important because they let us interact in an easy way with the database and we can use it with different platforms.

But why use Django Rest Framework and not Node.js, Golang, Rust, etc?

Well, for starters, DRF uses Python, an easy to learn language.

DRF, as much as Django, makes everything simple and easier. Do you know Django views? You have them in DRF too!

Do you want an end-point (an HTTP address) that returns you a list with each user, with all of their fields and another one to create new users? Cool, here’s the code:

class UserList(generics.ListCreateAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer

That’s it. You create a class called UserList that inherits methods from ListCreateAPIView.

As the name says, you can List and Create users. If you ask for the URL (GET), you get the list of the users, if you send (POST) information to that URL it will create a new user.

3 lines of code. That’s all you need.

Another great thing about DRF? Its documentation. Alongside with Scrapy, it has the best docs I have seen for a Django Framework.

There are more technical things like how migrations are handled, how each object has a model, how easy to switch databases is and more.

Rest assured, you can’t be wrong going with Django Rest Framework.


How to learn Django Rest Framework

Ok David, I’m sold. I want to learn Django Rest Framework right now so I can interact with my database easily. How I can learn DRF?

Good question! On top of being easy to use, you have a lot of free resources to learn it:


Conclusion

Let’s summarize everything we have learnt today about Rest APIs and the Django Framework:

  • REST APIs let us send information back and forth between an interface and a database.
  • We can use one REST API to feed any platform we want. This removes duplicity of code and helps to escalate it easily.
  • DRF uses Python, a language easy to learn.
  • Thanks to using Python and how DRF is structured, we only need a few lines to do anything.
  • The documentation is great
  • There is a lot of free resources on the internet
  • It has a great in-built interface

If you need to send and receive data between a mobile or web application, you can’t go wrong with Django Rest Framework.

Why don’t you start learning it right now?


My Youtube tutorial videos

Reach to me on Twitter

Read more posts