Open In App

Relational fields in Django models

Last Updated : 29 Oct, 2025
Comments
Improve
Suggest changes
24 Likes
Like
Report

Django models often represent real-world entities that are connected to each other. To manage these connections, Django provides fields to define relationships between models. Relationships allow models to link and share data.

Django supports three main types of relationships:

  • Many-to-One (ForeignKey)
  • Many-to-Many (ManyToManyField)
  • One-to-One (OneToOneField)

1. Many-to-one fields

A many-to-one relationship is used when one record of a model is related to multiple records of another model.

  • Defined using the ForeignKey field from django.db.models.
  • Name the many-to-one field using the related model’s name in lowercase.

Example:

Python
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)

In this example:

  • Song belongs to one Album.
  • An Album can have many Song records.
  • ForeignKey defines the many-to-one relationship.
  • on_delete=models.CASCADE deletes songs if the album is deleted.

2. Many-to-many fields

A many-to-many relationship is used when one record of a model is related to multiple records of another model and vice versa.

  • Defined using the ManyToManyField from django.db.models.
  • Name the many-to-many field using the plural version of the related model, lowercase.
  • The field can be placed in either model, but it should not be defined in both models.

Example:

Python
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)

In this example:

  • A Book can have multiple Authors.
  • An Author can write multiple Books.
  • ManyToManyField defines the many-to-many relationship.

3. One-to-one fields

A one-to-one relationship is used when one record of a model is related to exactly one record of another model. This is useful when an object extends another object in some way.

  • Defined using the OneToOneField from django.db.models.
  • Name the one-to-one field using the related model’s name, lowercase.

Example:

Python
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)

In this example:

  • Each Car is linked to exactly one Vehicle.
  • Each Vehicle can have at most one Car.
  • OneToOneField defines the one-to-one relationship.
  • on_delete=models.CASCADE deletes the Car if the linked Vehicle is deleted.

Data integrity options

When models depend on other models, it’s important to define what happens to related records when a record is deleted. This is controlled using the on_delete parameter in relational fields.

  • on_delete=models.CASCADE: Automatically deletes all related records. For example, deleting an Album will delete all related Song records.
  • on_delete=models.PROTECT: Prevents deletion of a record if it has related records. For example, attempting to delete an Album will be blocked if it has Songs.
  • on_delete=models.SET_NULL: Sets the relational field to NULL when the related record is deleted. Requires null=True.
  • on_delete=models.SET_DEFAULT: Sets the relational field to a default value when the related record is deleted. A default must be provided.
  • on_delete=models.SET(): Accepts a value or a callable. The return value is assigned to the field when the related record is deleted.
  • on_delete=models.DO_NOTHING: Takes no action. Generally considered bad practice.

Relationships between models
Visit Course explore course icon
Article Tags :

Explore