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: Django RestApis CRUD Application with Angular 6 & PostgreSQL tutorial
Django RestApi example Overview
Goal
The project create a set of Rest-APIs for GET/POST/UPDATE/DELETE APIs:
- GET
/customers/
: get all customers - GET
/customers/[id]
: get a customer byid
- POST
/customers/
: save a customer - PUT
/customers/update/[id]
: update a customer byid
- DELETE
/customers/[id]
: delete a customer byid
We will config the Project to work with PostgreSQL database.
Project structure
There are several folders and files in our Django project:
-
customers/apps.py: declares
CustomersConfig
class (subclass of thedjango.apps.AppConfig
) that represents our Django app and its configuration. -
gkzRestApi/settings.py: configures settings for the Django project, including
INSTALLED_APPS
list with Django REST framework and Customers Application. -
customers/models.py: defines
Customer
data model class (subclass of thedjango.db.models.Model
). -
migrations/0001_initial.py: is generated by
makemigrations
command, includes the code to create theCustomer
model, will be run bymigrate
to generate PostgreSQL database table forCustomer
model. -
customers/serializers.py: declares
CustomerSerializer
class (subclass ofrest_framework.serializers.ModelSerializer
) forCustomer
instances to manage serialization to JSON and deserialization from JSON. -
customers/views.py: contains methods to process HTTP requests and produce HTTP responses (using
CustomerSerializer
). -
customers/urls.py: defines
urlpatterns
to 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.
Setup Django RestApi project
More at: https://grokonez.com/django/django-restapis-postgresql-example-django-rest-framework-tutorial
Top comments (0)