Django + Angular 6 example | Django Rest Framework + MySQL CRUD example – Part 2: Django Server
Tags: django, crud, mysql, angular
Published: 2021-05-10 07:27:11

https://ozenero.com/django-angular-6-example-django-rest-framework-mysql-crud-example-part-2-django-server

Django + Angular 6 example | Django Rest Framework + MySQL CRUD example – Part 2: Django Server


This tutorial is part 2 of <a href="https://ozenero.com/frontend/angular/django-angular-6-example-django-rest-framework-mysql-crud-example-part-1-overview">Django-Angular-MySQL series</a>. Today, we will create Django server to do CRUD with MySQL (including finder method).

&gt;&gt; <a href="https://ozenero.com/frontend/angular/django-angular-6-example-django-rest-framework-mysql-crud-example-part-1-overview">Part 1: Overview</a>
&gt;&gt; <a href="https://ozenero.com/frontend/django-angular-6-example-django-rest-framework-angular-crud-mysql-example-part-3-angular-client">Part 3: Angular Client</a>

Related Post:
- <a href="https://ozenero.com/python/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</a>

<!--more-->
<h2>Video</h2>
<iframe width="853" height="480" src="https://www.youtube.com/embed/TEOoxcASn1k?rel=0" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="allowfullscreen"></iframe>
<h2>Django RestApi server Overview</h2>
<h3>Goal</h3>
Our Django Server can work with MySQL Database and provides 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>GET <code>/customers/age/[age]</code>: find all customers by <code>age</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>
 	<li>DELETE <code>/customers/</code>: delete all customers</li>
</ul>
<h3>Architecture</h3>
<img class="alignnone size-full wp-image-16864" src="https://ozenero.com/wp-content/uploads/2019/02/django-angular-6-django-rest-api-mysql-angular-server-architecture.png" alt="django-angular-6-django-rest-api-mysql-angular-server-architecture" width="672" height="305" />
<h3>Project structure</h3>
There are several folders and files in our Django project:

<img class="alignnone size-full wp-image-16860" src="https://ozenero.com/wp-content/uploads/2019/02/django-angular-6-django-rest-api-mysql-angular-django-project-structure.png" alt="django-angular-6-django-rest-api-mysql-angular-django-project-structure" width="279" height="353" />

- <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>
Django REST framework works on top of Django and helps us to build RESTful Web Services flexibly. To install this package, run command:
<code>pip install djangorestframework</code>
<h3>Create RestApi project</h3>
Create Django project named <strong>gkzRestApi</strong> with command:

More at:

https://ozenero.com/django-angular-6-example-django-rest-framework-mysql-crud-example-part-2-django-server

Django + Angular 6 example | Django Rest Framework + MySQL CRUD example – Part 2: Django Server