SoFunction
Updated on 2025-03-01

The implementation principle of django's ORM model

Introduction to ORM Model

As the project becomes larger and larger, a large number of SQL statements will appear in the code by writing native SQL, and the problem arises:

  1. The reuse rate of SQL statements is not high. The more complex the SQL statements, the longer the code is. Many similar SQL statements will appear.
  2. Many SQL statements are spelled out in business logic. If there is a database that needs to be changed, these logics need to be modified, which will easily miss the modification of certain SQL statements.
  3. It is easy to ignore web security issues when writing SQL, which poses hidden dangers to the future. SQL injection.

ORM, the full name Object Relational Mapping, is called object relational mapping in Chinese. Through ORM, we can operate the database through classes without writing native SQL statements. By mapping tables into classes, rows as instances, and fields as properties, ORM will eventually convert the corresponding operations into database native statements when performing object operations. There are many advantages to using ORM:

  • Ease of use: Using ORM for database development can effectively reduce the probability of repeating SQL statements, and the written model is also more intuitive and clear.
  • Small performance loss: ORM conversion to the underlying database operation instructions does have some overhead. However, from the actual situation, this performance loss is very small (less than 5%). As long as there are no strict requirements on performance, and taking into account the development efficiency and code reading ability, the benefits will be far greater than the performance loss, and the larger the project, the more obvious the effect.
  • Flexible design: It can easily write complex queries.
  • Portability: Django encapsulates the underlying database implementation and supports multiple relational database engines, including the popular MySQL, PostgreSQL, and SQLite. It is very easy to switch databases.

Create an ORM model

ORM models are usually placed in the app file. Each app can have its own model. And if this model wants to be mapped to the database, then the app must be installed in the INSTALLED_APP of . Here is a simple book ORM model. The sample code is as follows:

from  import models
class Book():
 name = (max_length=20,null=False)
 author = (max_length=20,null=False)
 pub_time = (default=)
 price = (default=0)

The above defines a model. This model inherits from . If this model wants to map to the database, it must be inherited from this class. This model is later mapped to the database, and the table name is the lowercase form of the model name, which is book. In this table, there are four fields, one of which is name. This field is stored as the name of the book, which is of type varchar, which cannot exceed 20 characters at most, and cannot be empty. The second field is the author name type, which is also the varchar type, and the length cannot exceed 20. The third is the publication time, the data type is the datetime type, and the default is the time to save the book. The fifth is the price of this book, which is the floating point type.

There is another field we have not written, which is the primary key id. In django, if a model does not define a primary key, an automatic growth int-type primary key will be automatically generated, and the name of this primary key is called id.

Map the model into the database:

Mapping the ORM model into the database is summarized in the following steps:

  1. In , configure DATABASES and make database-related configurations.
  2. Define a model in the app, and this model must be inherited from.
  3. Add this app to the INSTALLED_APP of .
  4. In the command line terminal, enter the path where the project is located, and then execute the command python makemigrations to generate the migration script file.
  5. Also on the command line, execute the command python migrate to map the migration script file to the database.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.