If you’ve used a digital device to send and receive information, you’ve used an API. Developers create APIs so that users can interact with data from their applications.
Creating a REST API is a convenient way to share information. REST APIs have defined standards that regulate data sharing between devices. To understand how REST API works, you can create one from scratch.
You can use Django REST Framework to build REST API and use it to display data from database.
Using Django with a REST API
You can use REST API to fetch structured data over HTTP. Like many languages and frameworks, Django lets you create your own API and consume others.
1. Install Django REST Framework
The Django REST Framework is a powerful toolkit that you can use to build and configure Web APIs. Its customizable features make it a popular choice for building REST APIs.
2. Create a Django App
The following instructions will explain how to create a food application to collect the names and descriptions of popular Kenyan foods. The API will receive requests from the database so that users can interact with that data.
Django apps come equipped with an SQLite database, so you don’t need to install any other databases.
3. Register the App Project Settings
Register the kenyanfood app in the project settings under the INSTALLED APPS array. If you skip this step, Django will not recognize the app. Also, register the Django REST framework in the same settings.
4. Register App URL
Register the kenyanfood app URL in the project urls.py file as shown below.
5. Create a View for the API
Create a dummy view in the app’s views.py file, so that the app doesn’t get any errors. First, import the response object and the @apiview decorator from the Django REST framework.
The response helps to return null data in JSON format while @apiview displays the API.
6. Create a URL path for the app
Create a URL path for the API view you created. This endpoint displays kenyanfood data.
Next, migrate the app to create tables in the SQLite database. You can do this by using the following command.
Successful migration means the database has created tables for kenyanfood app.
8. Add data to database
Use the Django Admin GUI to enter data into the database. Django Admin has a great interface for viewing and managing your application’s data.
Alternatively, you can use the Python shell on the command line to manually enter data into the database. In this guide, you will be using the Django admin interface.
After logging in, you’ll see the Django administration interface with the Group and User models. Both of these are for authentication; The food model is in the section below.
You can add and remove food items from the database from the admin page. Add some Kenyan cuisines like ugli, pilau and chai to the database.
You import the serializer module from the rest_framework package and create a FoodSerializer class that derives from the ModelSerializer class.
Next, specify the Food model you want to serialize and the fields you want to add to the API.
9. Update View
Next, update the API view with the serializer and food model.
First, define a GET method to retrieve all the data from the database with the Food.Objects.all() function. Then serialize the data and return it as a response in JSON format.