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.
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.
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.
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.
In the following section, we will see how ORM works. We will be using the following Django Models for demonstration:
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,
After creating and registering the models, we need to use the following command:
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:
This leads us to an interactive Python console. Next, we are supposed to import our models using the following command:
After this, we can perform ORM operations.
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:
We write the following code to create and save an object of Model Song:
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.
To retrieve all the objects of a model,all()is used:
<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.