Django Models featured image

Creating Django Models: A Tutorial

Django is a well-known web framework for the rapid development of secure and maintainable websites and web apps. Built using Python, Django simplifies web development, allowing more time and focus on writing apps without reinventing the wheel every single time. Django is a free and open-source project with robust official documentation, great community support, and plenty of free and paid-for support.

In this guide, we’ll dive into creating Django models. In Django, a model is the key source of info about your data. Models describe the fields and behavior of the data to be stored. Django uses the model to generate the database tables using ORM (Object Relational Mapping), also referred to as models.


We’re working on a Unix-based operating system, preferably an Ubuntu 20.04 cloud server as the system we’ve tested on. If you want to set up Django on a similar environment, please refer to our tutorial, “How To Install Django and Set Up a Development Environment on Ubuntu 20.04.”

Step 1. Python Development Environment

Django is built on Python. So, we need a robust Python development environment with all the necessary tools to work with Python apps. We’ve already discussed it in a dedicated guide. Here, we’ll have a short demonstration.

First, fire up a terminal and run the following commands:

Next, install pip – the Python package manager:

Then, install some additional development packages:

Django Models 1

To establish a virtual environment, we also need the Python venv module:

Step 2. Installing MySQL Server

MySQL is a popular relational database management system. It’s open-source, stable, and rich in features. We’ll use MySQL as the database server for our Django app. Install MySQL using APT:

Next, verify the installation:

Django Models 2

It’s just a barebone setup of MySQL with default options. Check out the more in-depth guide on MySQL installation and configuration.

Step 3. Setting Up the Django App

We’re now ready to create our Django app. If you’re interested in a further in-depth demonstration of Django, please refer to our tutorial, “Installing the Django Web Framework on Ubuntu 20.04.”

Next, create a dedicated directory for the app. The directory name should reflect the project name:

Within the project directory, create a Python virtual environment using the venv module:

Activate the virtual environment:

Django Models 3

Install Django using pip:

Next, create a new Django project using the django-admin command:


Step 4. Configuring the Django App

The Django app contains all the default configurations. For our goal, we need to make some adjustments.

Start by editing the configuration file

Then, change the time zone:


Add the machines that are allowed to access the Django app server:


After that, apply the changes:

Django Models 4

Finally, create a superuser for our Django web app:


Step 5. Connecting Django App to MySQL

To connect to MySQL, we need an additional Python connector library that’s compatible with Django. We’ll be using mysqlclient.

First, ensure that we have the necessary packages installed:


Next, install mysqlclient:

  • Creating a Dedicated Database

For our app, we need to create a dedicated database and database user. Launch the MySQL shell as root:

sudo mysql -u rootCreate a dedicated database for our Django app:

Then, create a dedicated database user for the Django app:

Finally, grant the dedicated user full permission on the dedicated database:

  • Changing Django Configuration to use MySQL

Now, we need to tweak some Django configurations so that it can use MySQL as the database server. Open up in a text editor:

Scroll down to the DATABASES section and paste the following code:


Next, edit the MySQL configuration file so that it contains the necessary database credentials:

Add the following block at the end of the file:

Django Models 5

Restart MySQL to take the changes into effect:

Step 6. Creating a Django Model

We’re now ready to create a new model within our Django project. Activate the virtual environment:

At this location, tell Django to create a new app:

After creating, the file tree should look something like this. Here, the file will contain the codes for our custom Django model:

Django Models 6

Check the current content of the file:


For demonstration, we’re going to create a model for blog posts named Post. It will contain the following fields:

  • title: Blog post title.
  • slug: Stores and generates valid URLs for web pages.
  • content: The blog post text.
  • created_on: Date of the creation of the post.
  • author: The user who created the post.

Open up the file in a text editor:

Let’s import some necessary APIs for our model. By default, it imports the models API. We’ll also be needing the slugify API for generating slugs, User for authentication, and reverse for a better and flexible URL creation:

Next, add the class method called Post. It will contain the necessary database fields like title, slug, content, created_on, and author:

We need the functionality for URL generation and saving the post. It’s important as it generates unique links matching to unique posts:

The next section will tell the model on ordering the post and displaying it on the web page. We’ll encode the logic using an inner class, Meta. It will contain additional model logic that doesn’t concern the database field definition:

Finally, we’ll have the feature of Comment model in this file. It requires adding another class Comment using models.Model as its signature. It will contain the following database fields:

  • name: Commenter’s name.
  • email: Commenter’s email address.
  • text: Content of the comment itself.
  • post: The post that comment was made on.
  • created_on: Time of the comment creation.

The code will look something like this:

At this point, our file will have a look like this:

Finally, save the file and close the editor.

Step 7. Updating Django Settings

The model is created. However, we need to tweak some configuration files to put the model in action. Inside, we need to add our model under INSTALLED_APPS.

Open in a text editor:

Register the new module under the section INSTALLED_APPS:


Save the file and close the editor.

Step 8. Migrating Changes

Now that we’ve added all the desired models, the next step is applying these changes. Django will apply these schemas to our MySQL database.

The following command will generate all the migration files necessary. The output will show the locations of all those files within the project directory:


The following command will list all the existing migrations:

Django Models 6

Note that all the migrations are marked except for 0001_initial that was created as a part of the models Post and Comment.

The following command will show what SQL query will Django execute to make the migrations:


All we need now is to apply the migrations. These migrations will write the changes into the database:


Step 9. Verifying Changes

To verify the changes, let’s have a look at our MySQL database. Start the MySQL shell as the Django user:

Next, change the current active database to the Django app database:

List all the tables from the database:

Lastly, to see the content of any of the tables, use the DESCRIBE query:

Django Models 7

Django Limitations with MySQL

When using MySQL as the database server for your Django app, there are a few things to keep in mind:

  • There’s not much support for transactions around schema-altering processes. Meaning, if a migration fails, it requires manually unpicking all the changes before trying another migration. There’s no way to roll back to an earlier point when the failed migration didn’t take place.
  • For most of the schema-altering processes, MySQL completely rewrites tables. In certain situations, it can be really time-consuming. At worst, the time complexity will be proportional to the number of rows to be added or columns to be removed. According to the official Django documentation, it could take one minute per million rows.
  • MySQL enforces a character limit for the name lengths of columns, tables, and indices. There’s also a limit on the total size of all columns and index covers. Django can go higher than the limit. Some backends other than MySQL can handle the higher limit of Django.

That’s why it’s strongly recommended to weigh your options and choose the appropriate one.

Final Thoughts

This guide successfully demonstrates how to add modules to an existing Django app. We used MySQL as the backend for the app. It also showcases some key Django concepts like models, how migration works, and how Django translates models into MySQL database tables.

Happy Computing!