Django RestApis example - GET/POST/PUT/DELETE requests to MongoDb database » grokonez
Tags: django, restapi, mongodb
Published: 2021-04-10 11:49:05

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

https://grokonez.com/django/django-restapis-mongodb-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 MongoDb Database using Django REST Framework.

<!--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>MongoDb</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-mongodb-example-django-rest-framework-tutorial-project-structure.png" alt="django-restapi-mongodb-example-django-rest-framework-tutorial-project-structure" width="279" height="353" class="alignnone size-full wp-image-16811" />

- <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 MongoDb 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>
<h3>Install Django REST framework</h3>
Django REST framework works on top of Django and helps us to build RESTful Web Services flexibly. To install this package, run command:


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