Build the API with Django Rest Framework

Tram Ho

Foreword

In recent times, I have learned about Python and Django. I want to share with you what I have learned and how I have created APIs with Django. Let’s go.

1. General introduction

Before we get into the code, let’s go through a bit of theory.

1.1. Restful

Concept

API ( A pplication P rogramming I nterface) is a set of rules and mechanisms by which an application or component will interact with another application or component. The API can return the data you need for your application in common data types like JSON or XML.

Restful ( RE presentational S tate T ransfer) is a type of data structure transformation, an architectural type for writing API. It uses a simple HTTP method to make communication between machines. So instead of using a URL for processing some user information, REST sends an HTTP request like GET, POST, DELETE, etc. to a URL to process the data.

RestAPI is a standard used in designing APIs for web applications to manage resources. RESTful is one of the most commonly used API design types today to let different applications (web, mobile …) communicate with each other.

The most important function of REST is to specify how to use HTTP methods (such as GET, POST, PUT, DELETE …) and how to format URLs for web applications to manage resources. RESTful does not specify the application code logic and is not limited by the application programming language, any language or framework can be used to design a RESTful API.

How Restful works

REST works primarily on the HTTP protocol. The above basic operations will use its own HTTP methods.

  • GET (SELECT): Returns a Resource or a Resource List.
  • POST (CREATE): Create a new Resource.
  • PUT (UPDATE): Updated information for Resource.
  • DELETE (DELETE): Delete a Resource.

These methods or operations are often referred to as CRUDs, which correspond to Create, Read, Update, Delete – Create, Read, Edit, Delete.

1.2. Diango Rest Framework

Django Rest Framework helps build RestAPI in Django in the most convenient way ^^

2. Install Django

Problem

I will write APIs to enable Create, Read, Update and Delete cars.

Car includes the following fields:

  • Name
  • Color
  • Brand

To create a django application, the first thing we need to do is install django on your device

2.1. Virtual environment

First, let’s consider creating a virtual environment for the project to be able to manage our packages independently.

Here pipenv will be used for your environment. You can refer to how to install pipenv here .

After pipenv installed, we run the command to access our virtual environment

2.2. Install the Django application

Install Django and Django REST Framework into the virtual environment

Install a new project with an application in it

And we get the project directory structure as follows:

With the project and application created above, first synchronize your database and create the first user and set a password for that user.

And when the installation is completed, you access 127.0.0.1:8000 and the result we get is?

2.2. Model

First, create a Model to store the data about Cars will be returned in the response. Open car/models.py file and enter the following code:

2.3. Serializer

2.4. View

2.5. URL

2.6. Setting

Once we have created a complete application, our job is to create the migration

2.7. Test API

And in the end, we enjoy the fruits:

When working with APIs, I often use Postman to check those APIs:

Create Car

Update Car

Delete Car

Get all Car

Epilogue

Above is the whole process when I started to learn and approach Django Rest. Anything wrong or incorrect, you guys please let me ask for bricks under the comment.

And the last wish wish you success in learning and learning about Django in general and Django Rest Framework in particular.

Related Links :

Share the news now

Source : Viblo