DEV Community


Django RestApis example - GET/POST/PUT/DELETE requests to MongoDb database » grokonez

loizenai profile image loizenai ・2 min read

Django RestApis example - GET/POST/PUT/DELETE requests to MongoDb database

In this tutorial, we’re gonna look at way to create Django RestAPIs with Get/Post/Put/Delete requests to interact with MongoDb Database using Django REST Framework.

Django RestApi example Overview


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 by id
  • POST /customers/: save a customer
  • PUT /customers/update/[id]: update a customer by id
  • DELETE /customers/[id]: delete a customer by id

We will config the Project to work with MongoDb database.

Project structure

There are several folders and files in our Django project:


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

Setup Django RestApi project

Install Django REST framework

Django REST framework works on top of Django and helps us to build RESTful Web Services flexibly. To install this package, run command:

Discussion (0)

Editor guide