In this tutorial, we’re gonna look at way to create Django RestAPIs with Get/Post/Put/Delete requests to interact with PostgreSQL Database using Django REST Framework.
Django RestApi example Overview
The project create a set of Rest-APIs for GET/POST/UPDATE/DELETE APIs:
/customers/: get all customers
/customers/[id]: get a customer by
/customers/: save a customer
/customers/update/[id]: update a customer by
/customers/[id]: delete a customer by
We will config the Project to work with PostgreSQL database.
There are several folders and files in our Django project:
CustomersConfigclass (subclass of the
django.apps.AppConfig) that represents our Django app and its configuration.
gkzRestApi/settings.py: configures settings for the Django project, including
INSTALLED_APPSlist with Django REST framework and Customers Application.
Customerdata model class (subclass of the
migrations/0001_initial.py: is generated by
makemigrationscommand, includes the code to create the
Customermodel, will be run by
migrateto generate PostgreSQL database table for
CustomerSerializerclass (subclass of
Customerinstances to manage serialization to JSON and deserialization from JSON.
customers/views.py: contains methods to process HTTP requests and produce HTTP responses (using
urlpatternsto be matched with request functions in the views.py.
- gkzRestApi/urls.py: defines root URL configurations that includes the URL patterns declared in customers/urls.py.