Python

Importance of ORM in Django Frameworks

KC Mahendra

KC Mahendra

17 April 2023 - 1 min read

ORM(object-relational mapper)

ORM is an acronym for the object-relational mapper. The ORM’s main goal is to transmit data between a relational database and an application model. The ORM automates this transmission, such that the developer need not write any SQL. The Django web framework includes a default object-relational mapping layer (ORM) that can be used to interact with data from various relational databases such as SQLite, PostgreSQL, MySQL, and Oracle. Django allows us to add, delete, modify, and query objects, using an API called ORM. An object-relational mapper provides an object-oriented layer between relational databases and object-oriented programming languages without having to write SQL queries. It maps object attributes to respective table fields. It can also retrieve data in that manner. This makes the whole development process fast and error-free.

ORM.png

In the above image, we have some Python objects and a table with corresponding fields. The object’s attributes are stored in corresponding fields automatically. An ORM will automatically create and store your object data in the database. You don’t have to write any SQL for the same.

The Problem Solved by ORM

ORMs have certain benefits over the traditional approach. The main advantage ORMs provide is rapid development. ORMs make projects more portable. It is easier to change the database if we use ORMs.

ORM_DATABASE_TABLE.png

In the past, web developers needed to have knowledge of databases too. A database has been an important component from the start. The programming languages used for web development use classes and objects for data interpretation. The class is used to define data structure in web applications. Then the same database schema is created in the database. This task requires skill and knowledge of SQL.

Knowing SQL is also not enough since SQL implementations slightly differ from one another in different databases. This became a difficult and time-consuming task. So, to resolve this, the concept of ORM was introduced in web frameworks. ORMs automatically create a database schema from defined classes/ models. They generate SQL from Python code for a particular database. ORMs let the developer build the project in one language which means Python. This increased the popularity of ORMs and web frameworks. These ORMs use connectors to connect databases with a web application. You must install the connector of a specific database you want to work with.

How the ORM works?

In the following section, we will see how ORM works. We will be using the following Django Models for demonstration:

djj4.PNG

In the above code, we created two models Album and Song. Whenever an instance of a model is created in Django, it will display the object as Modelname Object(1) in the admin interface. Hence to change the display name we use the function def str(self). The Strfunction in a Django model returns a string that is rendered as the display name of instances for that model. In our case, it will display the name of the title for the Model Album and the name of the song for the Model Song. In the Model Song, we are linking the second field, an album with the Model Album. Register the model in the admin.py file,

djj5.PNG

After creating and registering the models, we need to use the following command:

  • iPython manage.py make migrations
  • Python manage.py migrate

In the above commands, make migrations is responsible for packing up the changes into individual migration files, and migrate is responsible for applying those to our database. Now we need to access Django ORM, it can be accessed by using the following command inside our project directory:

In the above commands, makemigrations is responsible for packing up the changes into individual migration files, and migrate is responsible for applying those to our database. Now we need to access Django ORM, it can be accessed by using the following command inside our project directory:

  • python manage.py shell

This leads us to an interactive Python console. Next, we are supposed to import our models using the following command:

  • from playlist.models import Song, Album

After this, we can perform ORM operations.

Django ORM- Inserting, Retrieve Data:

Django lets us interact with its database models using an API called ORM. In this section, we will discuss some useful operations like adding, updating, and deleting data using Django ORM. • Adding Objects We write the following code to create and save an object of Model Album:

  • a = Album.objects.create(title="Add", artist="Sheeran", genre="Pop")
  • a.save()

We write the following code to create and save an object of Model Song:

  • s = Song.objects.create(name="Castle", album=a)
  • s.save()

Retrieving stands for getting the result of the search we make. So for retrieving the data in Django, let us add some records for ease of explanation.

Django, let us add some records for ease of explanation.

  • a = Album.objects.create(title="Add", artist="Sheeran", genre="pop")
  • a.save()
  • a = Album.objects.create(title="Abstract Road", artist="The Beatles", genre="rock")
  • a.save()
  • a = Album.objects.create(title="Run evolver", artist="The Beatles", genre="slow")
  • a.save()

To retrieve all the objects of a model,all()is used:

  • Album.objects.all()

<QuerySet [<Album: Add>, <Album: Abstract Road>, <Album: Run evolver>]>

The output is a set of objects that match the query. Since we used the str() function for the model Album we see the output has a title displayed for all the objects.

about the author

Full stack developer having 4 years of industrial experience in designing, development and maintenance of web apps based on Django-Python platforms.worked with different types of relational database(RDBMS) like: Sqlite,mysql,oracle