Django CRUD API with PostgreSQL

The Django application that uses the Django Rest Framework to build CRUD API with the PostgreSQL database.

Bharat Vora
Coding Blocks
4 min readFeb 17, 2021

--

Django CRUD API with PostgreSQL
Django Rest Framework

Django is extensively used in REST API nowadays. We can also use different frameworks or languages to build API. But, the only thing that everyone prefers Django because it is very easy to build and it is much faster.

Today, in this article, We are going to build the Django application that uses the Django Rest Framework to build CRUD API with the PostgreSQL database.

Before move forward, if you don’t know -
- What is API?
- What is REST Framework?
- What is the REST API?
Then you have to know this term first. Please refer to my article it takes only 5 mins to understand.

You can also refer to this article’s entire code at my GitHub account.

The URLs we’ll use to perform REST APIs —

URL pattern for CRUD API
URL pattern for CRUD API

Let’s Start Coding !

Start Coding

Make sure your system has all required packages -

Step-1: First, Isolate your development environment by creating a virtual environment. $ pipenv shell

Step-2: Install additional packages.

Step-3: Create a Django project and Django application.

Step-4: Update settings.py as shown below.

settings.py

Step-5: Define Django model according to your needs.

models.py

Step-6: Migrate data model by executing below command to PostgreSQL database.

If you want to run admin page or Django-auth then you must perform migration process without writing app’s name.

Step-7: Create a new file in app called serializers.py that will manage serialization and de-serialization from JSON. Add the below code.

serializers.py

Step-8: Add routes to urls.py so the server will response of client’s HTTP request.

Step-9: Add functions to views.py file.

views.py

We’re done here. You can run the server by python manage.py runserver .

Step-10: To check our CRUD API, we’re using Postman. Postman is API client that make developer’s work easier while working with APIs. If you don’t have Postman in your system then Download it from here.

Postman — API Client
  1. Let’s create some student entry first.
Add Student using POST

2. Retrieve all students information —

Retrieve all data by GET

3. Update student detail using ID —

Update detail by PUT

4. Retrieve student detail by using ID —

Ignore the middle body-row part

5. Delete student by using ID —

Delete single student using DELETE

6. Retrieve all the student, whom name contains a particular keyword —

Retrieve all students with specific keyword

7. Delete all students record —

Delete all students

Great :boom: !!!

We have successfully created the CRUD API in Django by using Django Rest Framework(DRF).

If you think, this article helped :100: you to increase your knowledge then please do clap :clap: and follow me and we’ll rise together in this knowledgeable world. :sunglasses:

If you want to be proficient in programming languages and latest technologies and If you are confused with Where to start the I have a best place for you.

Coding Blocks

Coding Blocks is the correct place to learn the programming languages and technologies of your choice. You can find here you interest courses like Web Development, Android, Software Training, Programming Courses, Java, Machine Learning, Algorithms, Data Structures, React Native, ReactJS, Artificial Intelligence, Deep Learning, Interview Preparation, Python, Product Management, and Data Science and etc.

You’ll also get the additional discount by using my Campus Ambassador code

50% of on all the course. Use code : CBCA760

--

--

Bharat Vora
Coding Blocks

Python Development | Machine Learning | AWS | Content Writing