Django Model implementation (Part II)

Octavio Lafourcade
4 min readMay 23, 2022

In the previous article, the implementation of the first Django template was done in an easy way by working with the views and templates. But, the MVT pattern considers the Model as an important part of a project as well.

Along with this article, a new project will be created called CodingProject. Within this project, an application called CodingApp will also be created.

Finally, in the application a Model will be deployed having these tables:

  1. Students (name, last name, email)
  2. Professor (name, last name, email, field)
  3. Task (name, delivery date, delivered)
  4. Course (name, course id)

Creating a new Project

Let's start positioning VSC in the path where the project will be created.

Then run the following instructions in the terminal:

django-admin startproject CodingProject

This will create a folder with the project.

Compare the implementation to this code:

https://github.com/tavolafourcade/django/commit/87a2d7c485d6cc5017d81cd1341ebcd9c55b235c

Creating a new Application

Go into the CodingProject folder with cd and create the application running:

python3 manage.py startapp CodingApp

This will create a second folder at the same level as the manage.py file.

The code corresponding to this section can be found here:

https://github.com/tavolafourcade/django/commit/eed1bad5397faec7299073dd012f4d97ed8a37b9

Creating the Model

Go into models.py from the CodingApp folder. In this file, each class corresponding to Students, Professor, Task, and Course will be created.

In this section, the code will be implemented manually as required by the DDL (Data Definition Language)

from django.db import modelsclass Student(models.Model):  name = models.CharField(max_length=40)  last_name = models.CharField(max_length=40)  email = models.EmailField()class Professor(models.Model):  name = models.CharField(max_length=40)  last_name = models.CharField(max_length=40)  email = models.EmailField()  field = models.CharField(max_length=40)class Task(models.Model):  name= models.CharField(max_length=30)  delivery_date = models.DateField()  delivered = models.BooleanField()class Course(models.Model):  name = models.CharField(max_length=40)  course_id = models.IntegerField()

Once done with the model structure implemented it’s time to add the application into settings.py.

So add CodingApp in the INSTALLED_APPS array with single quotes.

INSTALLED_APPS = [  'django.contrib.admin',  'django.contrib.auth',  'django.contrib.contenttypes',  'django.contrib.sessions',  'django.contrib.messages',  'django.contrib.staticfiles',  'CodingApp',]

So far so good, let’s check if what was done until this point is working properly with the following commands:

python3 manage.py check CodingApp

The expected output should be:

Check the Application for inconsistencies

Creating the Database

Now it’s time to migrate our Model into a Data Base using:

python3 manage.py makemigrations

The Database was correctly created but it is still empty.

First, it’s necessary to create the structure:

python3 manage.py sqlmigrate CodingApp 0001
The output of the implementation of the Database structure

These commands will make the following changes in the code:

https://github.com/tavolafourcade/django/commit/414f683c22644a1b719bdaf47ada504777e7d196

and:

python3 manage.py migrate
The output of the migration

These two instructions will make the trick and the database will be ready to work with.

Accesing the Database

We can see the content of the database using a UI Interface such as DB Browser.

Go to https://sqlitebrowser.org/, download and install the software.

Once installed, open it and go to Open database. Look for the file db.sqlite3 and you will see the tables created with the Model.

List of tables created in the Application

The content of each table can be accessed using the Browse Data tab (next to Database Structure).

Manipulating the Database

The database created is empty. Adding data in the tables requires to access the shell with the following commands:

python manage.py shell

Let’s import the

from CodingApp.models import Course

Then add a register with the fields declared in the Model

course = Course(name='Python', course_id=23850)

Finally, save the modification with:

course.save()

Refresh the software and the first register in the database can be found!

More registers can be added in the database in the same way or using New record tab in the UI Interface.

Once this is done, the shell can be exit with exit()

Adding registers to the table to be used in the view

Now, let’s create a register that can be used in the view.

Create a course function in which an instance of Course will be declared with the values for each field.

Save this instance and print it in the view.

from django.http import HttpResponsefrom CodingApp.models import Coursedef course(self):  course = Course(name='Web Development', course_id=24500)  course.save()  document = f'---> Course: {course.name} Course ID: {course.course_id}'  return HttpResponse(document)

Then, go to urls.py and establish the path to access the data from the table Course.

from django.contrib import adminfrom django.urls import pathfrom CodingApp.views import courseurlpatterns = [  path('admin/', admin.site.urls),  path('course/', course)]

Finally, go to the URL http://127.0.0.1:8000/course/

Also, go to the DB Browser software and refresh it and the data will be showed there as well.

Conclusion

There is a lot to go into this article but this foundation will serve to go deeper into the use of templates already created by third parties. So, don’t rush!

--

--