Django RestApis example – GET/POST/PUT/DELETE requests MySQL database with Django REST Framework
Tags: django, crud, restapi, mysql
Published: 2021-05-10 07:34:48

https://ozenero.com/django-restapi-mysql-example-get-post-put-delete-requests-django-rest-framework-tutorial

Django RestApis example – GET/POST/PUT/DELETE requests MySQL database with Django REST Framework

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

Related Post: <a href="https://ozenero.com/frontend/angular/django-angular-6-example-django-rest-framework-mysql-crud-example-part-1-overview">Django + Angular 6 example | Django Rest Framework + MySQL CRUD example</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/[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>MySQL</strong> database.
<h3>Project structure</h3>
There are several folders and files in our Django project:

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

- <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 MySQL 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>

More at:
https://ozenero.com/django-restapi-mysql-example-get-post-put-delete-requests-django-rest-framework-tutorial

Django RestApis example – GET/POST/PUT/DELETE requests MySQL database with Django REST Framework