Django CRUD API with PostgreSQL
The Django application that uses the Django Rest Framework to build CRUD API with the PostgreSQL database.
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 —
Let’s Start Coding !
Make sure your system has all required packages -
1. Python 3.8.x
2. Django 3.0.5
3. pipenv
Step-1: First, Isolate your development environment by creating a virtual environment.
$ pipenv shell
Step-2: Install additional packages.
$ pip install djangorestframework
#Help us to build RESTful web services flexibly. $ pip install psycopg2
#Python PostgreSQL adapter.$ pip install django-cors-headers
#Accept request from other origin(server).
Step-3: Create a Django project and Django application.
$ django-admin startproject DjangoRestCRUD$ cd DjangoRestCRUD$ python manage.py startapp restapp
Step-4: Update settings.py as shown below.
Step-5: Define Django model according to your needs.
Step-6: Migrate data model by executing below command to PostgreSQL database.
$ python manage.py makemigrations restapp$ python manage.py migrate restapp
Step-7: Create a new file in app called serializers.py that will manage serialization and de-serialization from JSON. Add the below code.
Step-8: Add routes to urls.py so the server will response of client’s HTTP request.
#DjangoRestCRUD/urls.pyfrom django.urls import path, includeurlpatterns = [
path('',include('restapp.urls')),
]------------------------------------------------------
#restapp/urls.py (create a new file)from django.conf.urls import url
from . import viewsurlpatterns = [
url(r'^api/student/$', views.student_list),
url(r'^api/student/(?P<pk>[0-9]+)$', views.student_details),
url(r'^api/student/gender$', views.student_gender),
]
Step-9: Add functions to views.py file.
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.
- Let’s create some student entry first.
2. Retrieve all students information —
3. Update student detail using ID —
4. Retrieve student detail by using ID —
5. Delete student by using ID —
6. Retrieve all the student, whom name contains a particular keyword —
7. Delete all students record —
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 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