Model’s Relationships
• There are three types of relationships between the models:
• One-to-one: we use models.OneToOneField
• Many-to-one: we use models.ForeignKey
• Many-to-many: we use models.ManyToManyField
• In every database relation there is a parent, and a child.
• The parent must be created before the child
• We define the relation field inside the child class
11
Common on_delete Options
We need to specify what will happen to the child if the parent is deleted using
on_delete option. Possible values:
• CASCADE: the associated child will be deleted.
• PROTECT: the data will be protected, and the parent will not be deleted
unless all referencing children are deleted first.
• SET_NULL: the child will not be deleted and the relative field referencing the
parent will be set to NULL (works only if the referencing field is nullable)
• SET_DEFAULT: the child will not be deleted and the relative field referencing
the parent will be set to a default value defined for the referenced field.
12 https://docs.djangoproject.com/en/4.1/ref/models/fields/#django.db.models.ForeignKey.on_delete
One-To-One Relationship
• to: specify the name of the parent class Customer
• Specify the on_delete option first_name Address
last_name 1 1
• Set primary_key = True. This ensures that Django will not
create a primary key. email street
birth_date city
• The “reverse” side of the relation will directly return a membership
single object.
• By default Django creates the reverse relation. In this case
it will be the name of the child class all in lower case.
(address)
• You can change it by specifying related_name option
• It is most useful as the primary key of a model which
“extends” another model in some way.
13 https://docs.djangoproject.com/en/4.1/topics/db/models/#one-to-one-relationships
A one-to-one relationship is similar to
a ForeignKey with unique=True, but the
"reverse" side of the relation will directly
Many-To-One Relationship return a single object. In contrast to
the OneToOneField "reverse" relation,
a ForeignKey "reverse" relation returns
a QuerySet.
• to: specify the name of the parent class Customer
• Specify the on_delete option first_name Address
last_name 1 *
• By default Django creates the reverse relation. In this
case it will be the name of the child class all in lower email street
case followed by _set. (address_set) birth_date city
membership
• You can change it by specifying related_name option
• For database representation, Django appends “_id” to
the field name to create its database column name
(customer_id)
• To create a recursive relationship – an object that has a
many-to-one relationship with itself – use:
14 https://docs.djangoproject.com/en/4.1/topics/db/models/#many-to-one-relationships
Many-To-Many Relationship
• to: specify the name of the related class Product
• By default Django creates the reverse relation. In this Promotion
case it will be the name of the class where we defined title * *
the relation all in lower case followed by _set. description description
(product_set)
price discount
• You can change it by specifying related_name option inventory
• For database representation, Django creates an
intermediary join table to represent the many-to-many
relationship. By default, this table name is generated
using the name of the many-to-many field and the name
of the table for the model that contains it.
15 https://docs.djangoproject.com/en/4.1/topics/db/models/#many-to-many-relationships
Resolving Circular Relationship
Product
• Sometimes, classes can have multiple relationships.
title Category
• They can be both depending on each other.
description * 1
• A class can be the parent class in one relation and a price name
child class in the other relation. inventory
• To resolve this circular dependency, we can use the
name of the model (class) instead of referencing the 0 .. 1
model itself to define the related model while featured_product
creating the relationship field.
• we might need to also, define the related_name
option so we don’t have a conflict with the reversed
relation.
• use related_name = ‘+’ to tell Django to not create
the reversed relationship.
16
Representing Inheritance
• A one-to-one relationship is most useful as
the primary key of a model which “extends”
another model in some way.
• Must specify primary_key = True
• Using multi-table inheritance, the inheritance
relationship introduces links between the
child model and each of its parents (via an
automatically-created OneToOneField)
• In both ways, each model in the hierarchy is
a model all by itself. Each model
corresponds to its own database table and
can be queried and created individually.
17 https://docs.djangoproject.com/en/4.1/topics/db/examples/one_to_one/#one-to-one-relationships
https://docs.djangoproject.com/en/3.2/topics/db/models/#multi-table-inheritance