Django ORM

Django’s Object-Relational Mapping (ORM) system, a fundamental component that bridges the gap between the database and the application’s code. This article delves into the intricate realm of Python, Django, and their ORM, uncovering how this technology stack simplifies database interactions and accelerates the development process. For demonstration purposes, we will use the following Django models.

Python3




class Album(models.Model):
    title = models.CharField(max_length = 30)
    artist = models.CharField(max_length = 30)
    genre = models.CharField(max_length = 30)
  
    def __str__(self):
        return self.title
  
class Song(models.Model):
    name = models.CharField(max_length = 100)
    album = models.ForeignKey(Album, on_delete = models.CASCADE)
  
    def __str__(self):
        return self.name


Django Shell

We can access the Django ORM by running the following command inside our project directory.

python manage.py shell

This brings us to an interactive Python console. Assuming that our models exist in

myProject/albums/models.py

we can import our models using the following command:

>>> from books.models import Song, Album

Django ORM – Inserting, Updating & Deleting Data

Django lets us interact with its database models, i.e. add, delete, modify, and query objects, using a database-abstraction API called ORM(Object Relational Mapper). This article discusses all the functional operations we can perform using Django ORM.

Prerequisite: Django models

Similar Reads

Django ORM

Django’s Object-Relational Mapping (ORM) system, a fundamental component that bridges the gap between the database and the application’s code. This article delves into the intricate realm of Python, Django, and their ORM, uncovering how this technology stack simplifies database interactions and accelerates the development process. For demonstration purposes, we will use the following Django models....

Django ORM Queries

...