Django 3 Tutorial & Example [2020]: Build a CRUD REST API for A Simple CRM

  • Author: Techiediaries Team
  • 03 Jan 2020

Django 3 is released with full async support!
In this tutorial series, you’ll learn about Django 3 by creating a CRUD example application with database, admin access, and REST API views. We’ll be using MySQL as the database system.
Throughout this beginner’s tutorial for Django 3, we are going to learn to build web applications with Python and Django. This tutorial assumes no prior experience with Django, so we’ll be covering the basic concepts and elements of the Django framework by emphasizing essential theory with practice.
Basically, we are going to learn Django fundamental concepts while building a simple CRM web application.
This tutorial doesn’t only cover fundamental basics of Django but also advanced concepts such as how to use and integrate Django with modern front end frameworks like Angular 2+, Vue and React.
You’ll learn about CRUD, database ORM, how to create API views and URLs.

What’s Django?

Django is an open source Python based web framework for building web applications quickly.

  • It’s a pragmatic framework designed for developers working on projects with strict dead lines.
  • It’s perfect for quickly creating prototypes and then continue building them after clients approval.
  • It follows a Model View Controller (MVC) design pattern
  • Django uses the Python language, a general purpose, powerful and feature-rich programming language.

    Django 3 Features Django 3 comes with many new features such as:

  • MariaDB support: Django now officially supports MariaDB 10.1+. You can use MariaDB via the MySQL backend,

  • ASGI support for async programming,
  • Django 3.0 provides support for running as an ASGI application, making Django fully async-capable
  • Exclusion constraints on PostgreSQL: Django 3.0 adds a new ExclusionConstraint class which adds exclusion constraints on PostgreSQL, etc.

    What’s MVC?

    MVC is a software architectural design pattern which encourages the separation of concerns and effective collaboration between designers and developers when working on the same project. It basically divides or separates your app into three parts:

  • Model: responsible for data storage and management,

  • View: responsible of representing and rendering the user interface or view,
  • Controller: responsible for handling logic to control the user interface and work with data model.

Thanks to MVC, you as a developer can work in the model and controller parts without being concerned with the user interface (left to designers) so if anything changes on the side of designers on the user interface, you can rest assured that you will not be affected.

Introduction to Python

Python is a general purpose programing language that’s suitable for developing all kind of applications including web applications. Python is known by a clean syntax and a large standard library which contains a wide range of modules that can be used by developers to build their applications instead of reinventing the wheel.
Here is a list of features and characteristics of Python:

  • Python is an Object Oriented Language just like Java or C++. Also like Java, Python is an interpreted language that runs on top of its own virtual machine which makes it a portable language that can runs across every machine and operating system such as Linux, Windows and MAC.
  • Python is especially popular among the scientific community where it’s used for creating numeric applications.
  • Python is also known by the great performance of its runtime environment which makes it a good alternative to PHP for developing web applications.

For more information you can head to http://python.org/ where you can also download Python binaries for supported systems.
For Linux and MAC, Python is included by default so you don’t have to install it. For Windows just head over to the official Python website and grab your installer. Just like any normal Windows program, the installation dead process is easy and straightforward.

Why Using Django?

Due to its popularity and large community, Python has numerous web frameworks among them Django. So what makes Django the right choice for you or your next project?

Django is a batteries-included framework

Django includes a set of batteries that can be used to solve common web problems without reinventing the wheel such as:

  • the sites framework,
  • the auth system,
  • forms generation,
  • an ORM for abstracting database systems,
  • and a very powerful templating engine,
  • caching system,
  • RSS generation framework etc.

    The Django ORM

    Django has a powerful ORM (Object Relational Mapper) which allows developers to use Python OOP classes and methods instead of SQL tables and queries to work with SQL based databases. Thanks to the Django ORM, developers can work with any database system such as MySQL or PostgresSQL without knowing anything about SQL. In the same time the ORM doesn’t get in the way. You can write custom SQL anytime you want especially if you need to optimize the queries against your server database for increased performance.

    Support for Internationalization: i18n

    You can use Django for writing web applications for other languages than English with a lot of ease thanks to its powerful support for internationalization or you can also create multi lingual websites

    The Admin Interface

    Django is a very suitable framework for quickly building prototypes thanks to its auto-generated admin interface.
    You can generate a full fledged admin application that can be used to do all sorts of CRUD operations against your database models you have registered with the admin module using a few lines of code.

    Community and Extensive Documentation

    Django has a great community that has contributed all sorts of awesome things to Django from tutorials and books to reusable open source packages that extend the core framework to include solutions for even more web development problems without reinventing the wheel or wasting time implementing what other developers have already created.
    Django has also one of the most extensive and useful documentation on the web which can gets you up and running with Django in no time.
    As a conclusion, if you are looking for a web framework full of features that makes building web applications fun and easy and that has all what you can expect from a modern framework. Django is the right choice for you if you are a Python developer.

  • Python is a portable programming language that can be used anywhere its runtime environment is installed.

  • Django is a Python framework which can be installed on any system which supports the Python language.

In this tutorial part, we are going to see how to install Python and Django on the major available operating systems i.e Windows, Linux and MAC.
At this point of our Django tutorial, we have covered what is Python, MVD and Django and the features of the Django. Let’s now see how to install Python and a local development environment with pip and venv.

Installing Python

Depending on your operating system you may or may not need to install Python. In Linux and MAC OS Python is included by default. You may only need to update it if the installed version is outdated.

Installing Python On Windows

Python is not installed by default on Windows, so you’ll need to grab the official installer from the official Python website at http://www.python.org/download/. Next launch the installer and follow the wizard to install Python just like any other Windows program.
Also make sure to add Python root folder to system path environment variable so you can execute the Python executable from any directory using the command prompt.
Next open a command prompt and type python. You should be presented with a Python Interactive Shell printing the current version of Python and prompting you to enter your Python commands (Python is an interpreted language)

Installing Python on Linux

If you are using a Linux system, there is a great chance that you already have Python installed but you may have an old version. In this case you can very easily update it via your terminal depending on your Linux distribution.
For Debian based distributions, like Ubuntu you can use the apt package manager

  1. sudo apt-get install python

This will update your Python version to the latest available version.
For other Linux distributions you should look for equivalent commands to install or update Python which is not a daunting task if you already use a package manager to install packages for your system then you should follow the same process to install or update Python.

Installing Python on MAC OS

Just like Linux, Python is included by default on MAC but in case you have an old version you should be able to update it by going to [http://www.python.org/download/mac/](http://www.python.org/download/mac/ and grab a Python installer for MAC.
Now if you managed to install or update Python on your own system or in case you have verified that you already have an updated version of Python installed on your system let’s continue by installing Django.

Installing PIP

PIP is a Python package manager which’s used to install Python packages from Python Package Index which is more advanced than easy_install the default Python package manager that’s installed by default when you install Python.
You should use PIP instaed of easy_install whenever you can but for installing PIP itself you should use easy_install. So let’s first install PIP:
Open your terminal and enter:

  1. $ sudo easy_install pip

You can now install Django on your system using pip

  1. $ sudo pip install django

While you can do this to install Django, globally on your system, it’s strongly not recommend. Instead you need to use a virtual environement to install packages.

Creating a MySQL Database

In this Django tutorial, we’ll be using a MySQL database. In your terminal invoke the mysql client using the following command:

  1. $ mysql -u root -p

Enter your MySQL password and hit Enter.
Next, run the following SQL statement to create a database:

  1. mysql> create database crmdb;

Django 3 Tutorial: Creating a Virtual Environment

Let’s start our tutorial by creating a virtual environment. Open a new terminal, navigate to a working folder and run the following command:

  1. $ cd ~/demos
  2. $ python3 -m venv .env

Next, activate the virtual environment using the following command:

  1. $ source .env/bin/activate

At this point of our tutorial, we’ve created a virtual environment for our Django project. Let’s now proceed to creating our project.

Django 3 Tutorial: Installing Django and Django REST Framework

Now, that you have created and activated your virtual environment, you can install your Python packages using pip. In your terminal where you have activated the virtual environment, run the following commands to install the necessary packages:

  1. $ pip install django
  2. $ pip install djangorestframework

You will also need to install the MySQL client for Python using pip:

  1. $ pip install mysqlclient

Django 3 Tutorial: Creating a Project

Now, let’s proceed to creating our django project. In your terminal, run the following command:

  1. $ django-admin startproject simplecrm

This command will take care of creating a bunch of necessary files for the project.
Executing the tree command in the root of our created project will show us the files that were created.

  1. .
  2. ├── simplecrm
  3. ├── __init__.py
  4. ├── settings.py
  5. ├── urls.py
  6. └── wsgi.py
  7. └── manage.py

__init__ is the Python way to mark the containing folder as a Python package which means a Django project is a Python package.
settings.py is the project configuration file. You can use this file to specify every configuration option of your project such as the installed apps, site language and database options etc.
urls.py is a special Django file which maps all your web app urls to the views.
wsgi.py is necessary for starting a wsgi application server.
manage.py is another Django utility to manage the project including creating database and starting the local development server.
These are the basic files that you will find in every Django project. Now the next step is to set up and create the database.
Next, open the settings.py file and update the database setting to point to our crmdb database:

  1. DATABASES = {
  2. 'default': {
  3. 'ENGINE': 'django.db.backends.mysql',
  4. 'NAME': 'crmdb',
  5. 'USER': 'root',
  6. 'PASSWORD': 'YOUR_DB_PASSWORD',
  7. 'HOST': 'localhost',
  8. 'PORT': '3306',
  9. }
  10. }

Next, add rest_framework to the INSTALLED_APPS array:

  1. INSTALLED_APPS = [
  2. # [...]
  3. 'rest_framework'
  4. ]

Finally, migrate the database using the following commands:

  1. $ cd simplecrm
  2. $ python manage.py migrate

You will be able to access your database from the 127.0.0.1:8000 address.

Django 3 Tutorial: Create an Admin User

Let’s create an admin user using the following command:

  1. $ python manage.py createsuperuser

Django 3 Tutorial: Creating an Application

Next, let’s create a Django application for encapsulating our core CRM functionality. In your terminal, run the following command:

  1. $ python manage.py startapp crmapp

Next, you need to add it in the settings.py file:

  1. INSTALLED_APPS = [
  2. # ...
  3. 'rest_framework',
  4. 'crmapp'
  5. ]

Django 3 Tutorial: Creating the Database Models

Let’s now proceed to create the database models for our application. We are going to create the following models:

  • Contact
  • Account
  • Activity
  • ContactStatus
  • ContactSource
  • ActivityStatus

We have three main models which are Contact, Account and Activity. The last three models are simply lookup tables (They can be replaced by an enum).
Open the crmapp/models.py file and the following code:

  1. from django.db import models
  2. from django.contrib.auth.models import User
  3. INDCHOICES = (
  4. ('FINANCE', 'FINANCE'),
  5. ('HEALTHCARE', 'HEALTHCARE'),
  6. ('INSURANCE', 'INSURANCE'),
  7. ('LEGAL', 'LEGAL'),
  8. ('MANUFACTURING', 'MANUFACTURING'),
  9. ('PUBLISHING', 'PUBLISHING'),
  10. ('REAL ESTATE', 'REAL ESTATE'),
  11. ('SOFTWARE', 'SOFTWARE'),
  12. )
  13. class Account(models.Model):
  14. name = models.CharField("Name of Account", "Name", max_length=64)
  15. email = models.EmailField(blank = True, null = True)
  16. phone = models.CharField(max_length=20, blank = True, null = True)
  17. industry = models.CharField("Industry Type", max_length=255, choices=INDCHOICES, blank=True, null=True)
  18. website = models.URLField("Website", blank=True, null=True)
  19. description = models.TextField(blank=True, null=True)
  20. createdBy = models.ForeignKey(User, related_name='account_created_by', on_delete=models.CASCADE)
  21. createdAt = models.DateTimeField("Created At", auto_now_add=True)
  22. isActive = models.BooleanField(default=False)
  23. def __str__(self):
  24. return self.name
  25. class ContactSource(models.Model):
  26. status = models.CharField("Contact Source", max_length=20)
  27. def __str__(self):
  28. return self.status
  29. class ContactStatus(models.Model):
  30. status = models.CharField("Contact Status", max_length=20)
  31. def __str__(self):
  32. return self.status
  33. class Contact(models.Model):
  34. first_name = models.CharField("First name", max_length=255, blank = True, null = True)
  35. last_name = models.CharField("Last name", max_length=255, blank = True, null = True)
  36. account = models.ForeignKey(Account, related_name='lead_account_contacts', on_delete=models.CASCADE, blank=True, null=True)
  37. email = models.EmailField()
  38. phone = models.CharField(max_length=20, blank = True, null = True)
  39. address = models.TextField(blank=True, null=True)
  40. description = models.TextField(blank=True, null=True)
  41. createdBy = models.ForeignKey(User, related_name='contact_created_by', on_delete=models.CASCADE)
  42. createdAt = models.DateTimeField("Created At", auto_now_add=True)
  43. isActive = models.BooleanField(default=False)
  44. def __str__(self):
  45. return self.first_name
  46. class ActivityStatus(models.Model):
  47. status = models.CharField("Activity Status", max_length=20)
  48. def __str__(self):
  49. return self.status
  50. class Activity(models.Model):
  51. description = models.TextField(blank=True, null=True)
  52. createdAt = models.DateTimeField("Created At", auto_now_add=True)
  53. contact = models.ForeignKey(Contact, on_delete=models.CASCADE, blank=True, null=True)
  54. def __str__(self):
  55. return self.description

Django 3 Tutorial: Creating Model Serializers

After creating models we need to create the serializers. In the crmapp folder create a serializers.py file:

  1. $ cd crmapp
  2. $ touch serializers.py

Next, open the file and add the following imports:

  1. from rest_framework import serializers
  2. from .models import Account, Activity, ActivityStatus, Contact, ContactSource, ContactStatus

Next, add a serializer class for each model:

  1. class AccountSerializer(serializers.ModelSerializer):
  2. class Meta:
  3. model = Account
  4. fields = "__all__"
  5. class ActivitySerializer(serializers.ModelSerializer):
  6. class Meta:
  7. model = Activity
  8. fields = "__all__"
  9. class ActivityStatusSerializer(serializers.ModelSerializer):
  10. class Meta:
  11. model = ActivityStatus
  12. fields = "__all__"
  13. class ContactSerializer(serializers.ModelSerializer):
  14. class Meta:
  15. model = Contact
  16. fields = "__all__"
  17. class ContactSourceSerializer(serializers.ModelSerializer):
  18. class Meta:
  19. model = ContactSource
  20. fields = "__all__"
  21. class ContactStatusSerializer(serializers.ModelSerializer):
  22. class Meta:
  23. model = ContactStatus
  24. fields = "__all__"

Django 3 Tutorial: Creating API Views After creating the model serializers, let’s now create the API views. Open the crmapp/views.py file and add the following imports:

  1. from rest_framework import generics
  2. from .models import Account, Activity, ActivityStatus, Contact, ContactSource, ContactStatus
  3. from .serializers import AccountSerializer, ActivitySerializer, ActivityStatusSerializer, ContactSerializer, ContactSourceSerializer, ContactStatusSerializer

Next, add the following views:

  1. from rest_framework import generics
  2. from .models import Account, Activity, ActivityStatus, Contact, ContactSource, ContactStatus
  3. from .serializers import AccountSerializer, ActivitySerializer, ActivityStatusSerializer, ContactSerializer, ContactSourceSerializer, ContactStatusSerializer
  4. class AccountAPIView(generics.ListCreateAPIView):
  5. queryset = Account.objects.all()
  6. serializer_class = AccountSerializer
  7. class ActivityAPIView(generics.ListCreateAPIView):
  8. queryset = Activity.objects.all()
  9. serializer_class = ActivitySerializer
  10. class ActivityStatusAPIView(generics.ListCreateAPIView):
  11. queryset = ActivityStatus.objects.all()
  12. serializer_class = ActivitySerializer
  13. class ContactAPIView(generics.ListCreateAPIView):
  14. queryset = Contact.objects.all()
  15. serializer_class = ContactSerializer
  16. class ContactStatusAPIView(generics.ListCreateAPIView):
  17. queryset = ContactStatus.objects.all()
  18. serializer_class = ContactSerializer
  19. class ContactSourceAPIView(generics.ListCreateAPIView):
  20. queryset = ContactSource.objects.all()
  21. serializer_class = ContactSourceSerializer

After creating these models, you need to create migrations using the following command:

  1. $ python manage.py makemigrations

Next, you need to migrate your database using the following command:

  1. $ python manage.py migrate

Creating API URLs

Let’s now create the API URLs to access our API views. Open the urls.py file and add the following imports:

  1. from django.contrib import admin
  2. from django.urls import path
  3. from crmapp import views

Next, add the following content:

  1. urlpatterns = [
  2. path('admin/', admin.site.urls),
  3. path(r'accounts', views.AccountAPIView.as_view(), name='account-list'),
  4. path(r'contacts', views.ContactAPIView.as_view(), name='contact-list'),
  5. path(r'activities', views.ActivityAPIView.as_view(), name='activity-list'),
  6. path(r'activitystatuses', views.ActivityStatusAPIView.as_view(), name='activity-status-list'),
  7. path(r'contactsources', views.ContactSourceAPIView.as_view(), name='contact-source-list'),
  8. path(r'contactstatuses', views.ContactStatusAPIView.as_view(), name='contact-status-list')
  9. ]

Django Tutorial: Enabling CORS

For development purposes, we’ll need to enable CORS (Cross Origin Resource Sharing) in our Django application.
So start by installing django-cors-headers using pip

  1. $ pip install django-cors-headers

Next, you need to add it to your project settings.py file:

  1. INSTALLED_APPS = (
  2. ## [...]
  3. 'corsheaders'
  4. )

Next, you need to add corsheaders.middleware.CorsMiddleware middleware to the middleware classes in settings.py

  1. MIDDLEWARE = (
  2. 'corsheaders.middleware.CorsMiddleware',
  3. # [...]
  4. )

You can then, either enable CORS for all domains by adding the following setting:

  1. CORS_ORIGIN_ALLOW_ALL = True

You can find more configuration options from the docs.

Starting the local development server

Django has a local development server that can be used while developing your project. It’s a simple and primitive server which’s suitable only for development not for production.
To start the local server for your project, you can simply issue the following command inside your project root directory:

  1. $ python manage.py runserver

Next navigate to the http://localhost:8000/ address with a web browser.
You should see a web page with a message:

  1. It worked!

Conclusion

To conclude this django 3 tutorial, let’s summarize what we have done. We have created a new Django project, created and migrated a MySQL database, built a simple CRM REST API with Django REST framework and started a local development server.