Setting up the
Database
Supported Database Engines
Django supports many database engines:
• SQLite: default database in Django
• Only use it for development and low
traffic websites
P
• PostgreSQL
• MySQL
• MariaDB
• Oracle
• MS SQL Server – 3rd party library
2
Creating Migrations
• Migrations are used by Django to generate the database
schema based on the models defined in models.py
• To create migrations, in the VSCode terminal run:
• Migration module is just a python module that describes
all the operations on the database.
• A new migration should be created whenever we change
anything in our models (add field, renaming field ..)
3
Renaming Migrations
• Migrations allow us to have a trace of the evolution of
the database
• .After creating migrations, you can rename it, however
you must ensure to change the name in all the
migration files that depend on this specific migration.
• General rule, keep your migrations small and specific
• To change the name provided by Django when creating migration you can
use the name option as follows:
python manage.py makemigrations --name <chosen_name> <app_name>
4
Adding Slug Field
• Slug is a newspaper term. It is a short label for
something, containing only letters, numbers,
underscores or hyphens.
• They’re generally used in URLs of website.
• It one of search engine optimization techniques, so
adding a slug make it easier for search engines to find
the content.
Actual question
5 Question ID (Slug)
Exercise
• Add slug field to the Product model.
• Create migration
• Rename the migration – (add_slug_to_product)
6
Running Migrations
• To generate the database schema, we need to run the created
migrations.
• On VSCode terminal run:
• The generated database can be found in db.sqlite3 of the
project
• db.sqlite3 file is created the first time you run either migrate or
runserver.
• runserver configure the database using Django’s default settings
7
SQLite Database
• In order to open db.sqlite3 we need to
install SQLite extension in VSCode.
• From the view menu choose command
pallete Note: table name is derived
• Type sqlite from:
<appname>_<modelname>
• Choose open database
• Choose the db.sqlite of the project
• This will add it the explorer as
SQLiteExplorer
8
Exercise
• Add field zip to the address model
• Create migration
• Run migration
• Check the migrations table
9
Customizing DB Schema
• Sometimes you need more control on the generated
table.
• Change table name
• Add index to help with search
• Ordering …
• To do so you need to add metadata to the model (class)
you want to change by adding an inner class called
Meta.
10
A class defines how an instance
of the class behaves while a meta
Model Meta class defines how a class behaves
• Model Meta is basically the inner class of your model
class.
• It is basically used to change the behavior of your
model fields like changing order options,
verbose_name, and a lot of other options.
(https://docs.djangoproject.com/en/4.1/ref/models/options/)
• It’s completely optional to add a Meta class to your
model.
11
Exercise
• Add a Meta class in Customer model to:
• Change table name to ‘strore_customers’
• Add Indexes field ‘first_name’ and ‘last_name’
• Create and run migrations
12
• To reverse all migrations applied for an app,
use the name zero:
Reverting Migrations python manage.py migrate <app_name> zero
• You can revert (undo) migrations in different ways depending on the
changes that you want to undo
• If you want to selectively undo some changes
• Erase the changes you don’t want
• Create new migration and run it
• If you want to completely undo all the changes made by last migration.
• In VSCode terminal run:
python manage.py migrate <app name> <prev_migration_numer_you_want>
• Delete the unwanted last migration file
• Manually remove all the code you changed in last migration.
13
Exercise
• Change the name of ‘description’ field in
Product model to ‘details’
• Create migration
• Run migration
• Revert the last migration
14
Installing MySQL
• Go to https://www.mysql.com/
• Follow the instructions on moodle to install mySQL
15
Connecting to
MySQL
• We need a tool to connect to mySQL and
mange the database (MySQL Client): we
will use MySQL Workbench
• Open MySQL Workbench and enter your
password
• Open a new query file and create a
database for the project
• After executing the query, you will have
your new database
16
Connecting Django
to MySQL
• Check if your installation of mySQL is working by
typing in a new VSCode terminl
• Install mysqlclient package inside your project by
running this command in VSCode terminal
• Change Database setting in settings.py
• Make sure server still running
• Run the migrations so all tables are created in
MySQL
17
Generating
Dummy Data
• Visit
https://www.mockaroo.com/
18
• Middleware is a framework of hooks into
Django’s request/response processing.
• It’s a light, low-level “plugin” system for
Adding Django Debug Toolbar •
globally altering Django’s input or output.
Each middleware component is
responsible for doing some specific
function
Go to https://django-debug-toolbar.readthedocs.io/en/latest/installation.html and follow instructions as follow
1. Open a new terminal in VSCode
2. pipenv install django-debug-toolbar
3. add ‘debug_toolbar’ in the list of INSTALLED_APPS in settings of the project
4. add the debug toolbar to the URLCon of the project as follows:
1. open urls.py inside the project folder
2. import debug_toolbar
3. add another URL inside urlpatterns:
5. add a middleware for the debug_toolbar at the top inside the list of MIDDLEWARE in the project settings file.
6. add the IP of your website in the list of INTERNAL_IPS, you should add the whole settings to the settings file of your project. In
development the IP address is local and it is the localhost address:
19
20