Python | Relational fields in Django models
Last Updated :
07 Jul, 2019
Prerequisite:
Django models
Â
Django models represent real-world entities, and it is rarely the case that real-world entities are entirely independent of each other. Hence Django supports relational databases and allows us to establish relations between different models. There are three types of relational fields which Django supports: many-to-one, many-to-many and one-to-one.
Many-to-one fields:
This is used when one record of a model A is related to multiple records of another model B. For example - a model
Song has many-to-one relationship with a model
Album, i.e. an album can have many songs, but one song cannot be part of multiple albums. Many-to-one relations are defined using
ForeignKey
field of
django.db.models
.
Below is an example to demonstrate the same.
Python3
from django.db import models
class Album(models.Model):
title = models.CharField(max_length = 100)
artist = models.CharField(max_length = 100)
class Song(models.Model):
title = models.CharField(max_length = 100)
album = models.ForeignKey(Album, on_delete = models.CASCADE)
It is a good practice to name the many-to-one field with the same name as the related model, lowercase.
Many-to-many fields:
This is used when one record of a model A is related to multiple records of another model B and vice versa. For example - a model
Book has many-to-many relationship with a model
Author, i.e. an book can be written by multiple authors and an author can write multiple books. Many-to-many relations are defined using
ManyToManyField
field of
django.db.models
.
Below is an example to demonstrate the same.
Python3
from django.db import models
class Author(models.Model):
name = models.CharField(max_length = 100)
desc = models.TextField(max_length = 300)
class Book(models.Model):
title = models.CharField(max_length = 100)
desc = models.TextField(max_length = 300)
authors = models.ManyToManyField(Author)
It is a good practice to name the many-to-many field with the plural version of the related model, lowercase. It doesn't matter which of the two models contain the many-to-many field, but it shouldn't be put in both the models.
One-to-one fields:
This is used when one record of a model A is related to exactly one record of another model B. This field can be useful as a primary key of an object if that object extends another object in some way. For example - a model
Car has one-to-one relationship with a model
Vehicle, i.e. a car is a vehicle. One-to-one relations are defined using
OneToOneField
field of
django.db.models
.
Below is an example to demonstrate the same.
Python3
from django.db import models
class Vehicle(models.Model):
reg_no = models.IntegerField()
owner = models.CharField(max_length = 100)
class Car(models.Model):
vehicle = models.OneToOneField(Vehicle,
on_delete = models.CASCADE, primary_key = True)
car_model = models.CharField(max_length = 100)
It is a good practice to name the one-to-one field with the same name as that of the related model, lowercase.
Data integrity options:
Since we are creating models which depend on other models, we need to define the behavior of a record in one model when the corresponding record in the other is deleted. This is achieved by adding an optional
on_delete
parameter in the relational field, which can take the following values:
on_delete = models.CASCADE
- This is the default value. It automatically deletes all the related records when a record is deleted.(e.g. when an Album record is deleted all the Song records related to it will be deleted)
on_delete = models.PROTECT
- It blocks the deletion of a record having relation with other records.(e.g. any attempt to delete an Album record will be blocked)
on_delete = models.SET_NULL
- It assigns NULL to the relational field when a record is deleted, provided null = True
is set.
on_delete = models.SET_DEFAULT
- It assigns default values to the relational field when a record is deleted, a default value has to be provided.
on_delete = models.SET()
- It can either take a default value as parameter, or a callable, the return value of which will be assigned to the field.
on_delete = models.DO_NOTHING
- Takes no action. Its a bad practice to use this value.
Similar Reads
Built-in Field Validations - Django Models
Field validations in Django help make sure the data you enter into your database is correct and follows certain rules. Django automatically checks the data based on the type of field you use, so you donât have to write extra code to validate it yourself.Django provides built-in validations for every
3 min read
Custom Field Validations in Django Models
Field validation ensures that the data entered into a model field meets specific rules before itâs saved to the database. While Django provides built-in validations for common checks, custom field validation lets you enforce your own rules, such as verifying formats, length limits, or complex condit
3 min read
Intermediate fields in Django | Python
Prerequisite: Django models, Relational fields in DjangoIn Django, a many-to-many relationship is used when instances of one model can be associated with multiple instances of another model and vice versa. For example, in a shop management system:A Customer can purchase multiple Items.An Item can be
2 min read
Python | Uploading images in Django
Prerequisite - Introduction to DjangoUploading and managing image files is a common feature in many web applications, such as user profile pictures, product images, or photo galleries. In Django, you can handle image uploads easily using the ImageField in models.In this article, weâll walk through a
3 min read
Relational Model in DBMS
The Relational Model represents data and their relationships through a collection of tables. Each table also known as a relation consists of rows and columns. Every column has a unique name and corresponds to a specific attribute, while each row contains a set of related data values representing a r
11 min read
Add the slug field inside Django Model
The slug field within Django models is a pivotal step for improving the structure and readability of URLs in web applications. This addition allows developers to automatically generate URL-friendly slugs based on titles, enhancing user experience and search engine optimization (SEO). By implementing
4 min read
null=True - Django Built-in Field Validation
Built-in Field Validations in Django models are the default validations that come predefined to all Django fields. Every field comes in with built-in validations from Django validators. One can also add more built-in field validations for applying or removing certain constraints on a particular fiel
4 min read
primary_key - Django Built-in Field Validation
Built-in Field Validations in Django models are the default validations that come predefined to all Django fields. Every field comes in with built-in validations from Django validators. One can also add more built-in field validations for applying or removing certain constraints on a particular fiel
4 min read
related_name - Django Built-in Field Validation
The related_name attribute specifies the name of the reverse relation from the User model back to your model. If you don't specify a related_name, Django automatically creates one using the name of your model with the suffix _set. Syntax: field_name = models.Field(related_name="name") Explanation: I
3 min read
Model Fieldname restrictions in Django Framework
A Django model is the built-in feature that Django uses to create tables, their fields, and various constraints. In short, Django Models is the SQL of Database one uses with Django. SQL (Structured Query Language) is complex and involves a lot of different queries for creating, deleting, updating or
2 min read