Django ORM — Pros and Cons

Sohaib Anser
3 min readMay 27, 2021

--

In this article, you will learn…

What is Django ORM?
What are its Pros and cons?
Which one is a better approach for your next web application? ORM or SQL.

Django ORM stands for object-relational mapper used to interact with databases. It is more of a pythonic way to interact with databases rather than writing SQL queries. The queried data from the database is converted to a python object which is much easier to manipulate.

ORM uses Model classes to define database schema. The Model-based schema is transformed to database tables through the migration process. The Model is basically a pythonic way to define the schema.
To run the migration uses the following commands in the defined order.

python manange.py makemigrations

python manange.py migrate

After migration, you can interact with the database through the shell. Access the shell using the command

run python manage.py shell

It's obvious that ORM is an extra layer of computation to manipulate data. But still, most of the developers use it rather than SQL queries. There are many reasons to use it. First, it provides a faster development track with error-free code. It's easy to maintain the code once development is complete.

In some applications, you might need to interact with multiple databases and each database has its own SQL syntax. So, if you go with the SQL query writing approach, the development process will slow down with more errors, and also code maintenance will become a challenge in the future. By choosing ORM, you can avoid these problems with a quick development strategy.

Security is the main concern of web applications and SQL injection is a major vulnerability to this security. It is one of the top 10 web application security risks by OWASP (Open Web Application Security Project).

Proper implementation of SQL queries is mandatory to mitigate the risks of SQL injection. But ORM provides an extra shield of security to protect the SQL injections. You do not need to worry about it, ORM handle
it on its own side.

ORM became a standard, not with Django only but it's available with almost every web framework. With all these features, ORM comes with an extra overhead of complexity. Sometimes it's very difficult to write the optimized query with ORM due to its limitations. As ORM translates the pythonic written query to SQL syntax to interact with the Database and few times it produces a very complex SQL query that requires more time for computation than actual. It results in a decrease in performance.

Obviously, Django ORM comes with exceptional features and one must prefer to use it rather than writing SQL queries. For complexity and performance reasons you can use the RAW query option wherever required. Only choose the SQL queries option, if it’s a strict requirement of the project that can not be fulfilled by the ORM.

--

--

Sohaib Anser

Backend Engineer, Python, AWS, Committed to making a difference. Follow me on LinkedIn: https://www.linkedin.com/in/muhammad-sohaib-python