Django REST framework is a powerful and flexible toolkit for building Web APIs. REST is a loosely defined protocol for listing, creating, changing, and deleting data on your server over HTTP. The Django REST framework (DRF) is a toolkit built on top of the Django web framework that reduces the amount of code you need to write to create REST interfaces.
API services allow applications to connect to other applications using JSON-encoded data. Create an API once, and consume it from any API client or front-end application.
Modern web applications are increasingly built as Single-Page-Applications (SPAs) which feature a distinct frontend and backend. As opposed to the traditional monolith approach in Django itself–and other web frameworks like Ruby on Rails.
SPAs require a backend API that can then be consumed by multiple frontends as needed.
The downside is that it takes more time and code to create a separate frontend/backend for projects.
Many Companies using Django Rest Framework including some of most popular are Robinhood, UpstageAI, and Bepro Company
You’ll also need some experience with Python and Django.
If you want to try the django rest framework, you can do a quickstart or follow a detailed tutorial.
Create a new Django project named tutorial, then start a new app called quickstart.
It may look unusual that the application has been created within the project directory. Using the project’s namespace avoids name clashes with external modules (a topic that goes outside the scope of the quickstart).
Now sync your database for the first time:
We’ll also create an initial user named admin with a password of password123. We’ll authenticate as that user later in our example.
Once you’ve set up a database and the initial user is created and ready to go, open up the app’s directory and we’ll get coding…
First up we’re going to define some serializers. Let’s create a new module named tutorial/quickstart/serializers.py that we’ll use for our data representations.
Notice that we’re using hyperlinked relations in this case with HyperlinkedModelSerializer. You can also use primary key and various other relationships, but hyperlinking is good RESTful design.
Right, we’d better write some views then. Open tutorial/quickstart/views.py and get typing.
Rather than write multiple views we’re grouping together all the common behavior into classes called ViewSets.
We can easily break these down into individual views if we need to, but using viewsets keeps the view logic nicely organized as well as being very concise.
Okay, now let’s wire up the API URLs. On to tutorial/urls.py
Because we’re using viewsets instead of views, we can automatically generate the URL conf for our API, by simply registering the viewsets with a router class.
Again, if we need more control over the API URLs we can simply drop down to using regular class-based views, and writing the URL conf explicitly.
Finally, we’re including default login and logout views for use with the browsable API. That’s optional, but useful if your API requires authentication and you want to use the browsable API.
Pagination allows you to control how many objects per page are returned. To enable it add the following lines to tutorial/settings.py
Add ‘rest_framework’ to INSTALLED_APPS. The settings module will be in tutorial/settings.py
Okay, we’re done.
We’re now ready to test the API we’ve built. Let’s fire up the server from the command line.
We can now access our API, both from the command-line, using tools like curl…