Django RestApis example – GET/POST/PUT/DELETE requests to PostgreSQL database

Tags: django, restapi, postgres
Published: 2021-04-10 11:23:08

https://grokonez.com/django/django-restapis-postgresql-example-django-rest-framework-tutorial



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.

Related Post: <a href="https://grokonez.com/frontend/angular/angular-6/django-restapis-crud-application-angular-6-postgresql-tutorial">Django RestApis CRUD Application with Angular 6 & PostgreSQL tutorial</a>

<!--more-->
<h2>Django RestApi example Overview</h2>
<h3>Goal</h3>
The project create a set of Rest-APIs for GET/POST/UPDATE/DELETE APIs:
<ul>
<li>GET <code>/customers/</code>: get all customers</li>
<li>GET <code>/customers/[id]</code>: get a customer by <code>id</code></li>
<li>POST <code>/customers/</code>: save a customer</li>
<li>PUT <code>/customers/update/[id]</code>: update a customer by <code>id</code></li>
<li>DELETE <code>/customers/[id]</code>: delete a customer by <code>id</code></li>
</ul>
We will config the Project to work with <strong>PostgreSQL</strong> database.
<h3>Project structure</h3>
There are several folders and files in our Django project:

<img src="https://grokonez.com/wp-content/uploads/2019/02/django-restapi-postgresql-example-django-rest-framework-tutorial-project-structure.png" alt="django-restapi-postgresql-example-django-rest-framework-tutorial-project-structure" width="279" height="353" class="alignnone size-full wp-image-16785" />

- <em><strong>customers/apps.py</strong></em>: declares <code>CustomersConfig</code> class (subclass of the <code>django.apps.AppConfig</code>) that represents our Django app and its configuration.
- <em><strong>gkzRestApi/settings.py</strong></em>: configures settings for the Django project, including <code>INSTALLED_APPS</code> list with <em>Django REST framework</em> and <em>Customers Application</em>.
- <em><strong>customers/models.py</strong></em>: defines <code>Customer</code> data model class (subclass of the <code>django.db.models.Model</code>).
- <em><strong>migrations/0001_initial.py</strong></em>: is generated by <code>makemigrations</code> command, includes the code to create the <code>Customer</code> model, will be run by <code>migrate</code> to generate PostgreSQL database table for <code>Customer</code> model.
- <em><strong>customers/serializers.py</strong></em>: declares <code>CustomerSerializer</code> class (subclass of <code>rest_framework.serializers.ModelSerializer</code>) for <code>Customer</code> instances to manage serialization to JSON and deserialization from JSON.
- <em><strong>customers/views.py</strong></em>: contains methods to process HTTP requests and produce HTTP responses (using <code>CustomerSerializer</code>).
- <em><strong>customers/urls.py</strong></em>: defines <code>urlpatterns</code> to be matched with request functions in the <em><strong>views.py</strong></em>.
- <em><strong>gkzRestApi/urls.py</strong></em>: defines root URL configurations that includes the URL patterns declared in <em><strong>customers/urls.py</strong></em>.

<h2>Setup Django RestApi project</h2>

More at: https://grokonez.com/django/django-restapis-postgresql-example-django-rest-framework-tutorial