Django + Angular 6 example | Django Rest Framework + MySQL CRUD example – Part 2: Django Server
This tutorial is part 2 of Django-Angular-MySQL series. Today, we will create Django server to do CRUD with MySQL (including finder method).
>> Part 1: Overview
>> Part 3: Angular Client
Related Post:
Video
Django RestApi server Overview
Goal
Our Django Server can work with MySQL Database and provides APIs:
- GET
/customers/: get all customers - GET
/customers/[id]: get a customer byid - GET
/customers/age/[age]: find all customers byage - POST
/customers/: save a customer - PUT
/customers/[id]: update a customer byid - DELETE
/customers/[id]: delete a customer byid - DELETE
/customers/: delete all customers
Architecture
Project structure
There are several folders and files in our Django project:
-
customers/apps.py: declares
CustomersConfigclass (subclass of thedjango.apps.AppConfig) that represents our Django app and its configuration. -
gkzRestApi/settings.py: configures settings for the Django project, including
INSTALLED_APPSlist with Django REST framework and Customers Application. -
customers/models.py: defines
Customerdata model class (subclass of thedjango.db.models.Model). -
migrations/0001_initial.py: is generated by
makemigrationscommand, includes the code to create theCustomermodel, will be run bymigrateto generate MySQL database table forCustomermodel. -
customers/serializers.py: declares
CustomerSerializerclass (subclass ofrest_framework.serializers.ModelSerializer) forCustomerinstances 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
urlpatternsto 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
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:pip install djangorestframeworkCreate RestApi project
Create Django project named gkzRestApi with command:
More at:
Django + Angular 6 example | Django Rest Framework + MySQL CRUD example – Part 2: Django Server


Top comments (0)