Chapter 3. Django Models and Databases

Django Models and Databases Guide

This chapter covers how to create a database for Django applications. In the first few sections, we’ll explain the critical steps to create a database, and in the later part of this chapter, we’ll explain how to edit the models.py file in detail.

What We Cover in This Chapter

The following topics are covered in this chapter:

Create a Database in Django

In this section, we'll go over the essential steps to create a database in Django: defining models in the models.py file, creating migration files, and applying migrations to generate tables in the database.

Relational Database

We’ll explore relational databases and their key concepts, such as tables, fields, records, and relationships. We’ll also define the different types of relationships: one-to-many, one-to-one, and many-to-many.

Create Django Models

This section introduces how to define models in Django using Python classes. We’ll demonstrate how to define fields with different data types and options, including help text, choices, and relationships.

Makemigrations and Migrate

In this section, we'll cover the makemigrations and migrate commands, which allow you to create and apply database migrations in Django, ensuring that your database schema matches your models.

Add Models in Django Admin – admin.py

This section explains how to register your Django models in the Django admin panel, making them accessible for CRUD operations directly from the Django admin interface.

Change Display Name of Record Objects

We’ll learn how to customize the way records are displayed in the Django admin by using the __str__() method in the model definition.

Django Models – Data Field Type

Here, we'll introduce key data field types in Django models, including CharField, TextField, IntegerField, and others. We'll also explore their options and when to use each field type.

Django Models – Field Options

We’ll dive deeper into field options such as blank, unique, and default, which help define the behavior of each field in the database.

Django Models – Help Text Option

This section explains how to use the help_text option to provide guidance for users in the Django admin interface when entering data for each field.

Django Models – Choices Option

We’ll cover how to use the choices option in Django models to create a dropdown list of choices for a field, and how to display the selected choice in a human-readable format.

Django Models – DateField with datetime Module

In this section, we’ll learn how to use the DateField along with the datetime module to set date values in our models, along with options like auto_now_add and auto_now.

Django Models – Relationship Fields

This section covers how to establish relationships between Django models using ForeignKey, OneToOneField, and ManyToManyField.

Django Models – ID

We’ll explore how Django automatically adds an id field to models as a primary key and how to use it for defining relationships between models.

Django Models – ForeignKey (OneToMany Relationship)

This section explains how to create a one-to-many relationship using ForeignKey and configure the on_delete behavior for related models.

Django Models – OneToOneField

In this section, we’ll cover how to create a one-to-one relationship between models using OneToOneField and how to define the primary key for such relationships.

Django Models – ManyToManyField

We’ll learn how to define a many-to-many relationship between models using ManyToManyField, and Django’s automatic creation of an intermediary join table.

Learn offline for better focus!
A book for this course is available on Amazon.

Django Visual Guide

The Complete Beginner's Handbook to Mastering Web Development Effortlessly Without Prior Programming Experience

Get the Book Now

Django Introduction
Course Content