Introduction to the Django ORM

In our Django series, Eliga Services discusses how an Object-Relational Mapper (ORM) works and more specifically the benefits of Django’s ORM.

 

What is an ORM?

An ORM stands for Object-Relational Mapper. It enables you to interact with your database. This allows you to avoid writing complicated SQL, helping you query and manipulate your database to get results. This clever engineering takes advantage of the more complex aspects of Python to make developers’ lives easier.

 

What is Django’s ORM?

When people speak of the Django ORM, they are referring to a library, which implements ORM. This code library automates the transfer of data stored in relational database tables into objects that are more commonly used in application code.

According to Nensi Trambadiya, it’s ‘a powerful and elegant way to interact with databases.’ We think this is an apt description and way of thinking of Django ORM.

 

Does an ORM library have to be written in Python Django?

No, an ORM library can be written in your developer’s language of choice. By encapsulating the code needed to manipulate the data, developers are freed from using SQL to interact directly with the object in the language they’re using.

 

What are the benefits of Django ORM for developers?

Django’s ORM saves developers a lot of time because it allows them to write their data model in a single place, making it easier to update, maintain and reuse code. It also allows for the automatic handling of database schemas.

Best of all, your Web Programmers don’t have to write SQL. As most Web Programmers treat SQL like a sub-language, this can result in poorly formed and hard-to-maintain SQL. According to Haitian Wei, an ORM allows for ‘sanitizing; using prepared statements or transactions are as easy as calling a method.’

 

Getting started with ORM…

It may sound obvious, but to get started with ORM, you’ll need a database to manipulate, defining a bunch of tables and their relationships.

If this sounds complicated, don’t worry it’s a lot simpler than it sounds. This helpful video by LaunchCode.org gives a general overview in a ‘language agnostic way’ for anyone who wants to learn more.

What is an ORM query in Django?

Once you’ve created your data models, you’ll be able to retrieve any objects you’ve created. You can also update and delete objects, using Python. You can learn about constructing a QuerySet with Django’s official documentation here.

 

Is ORM slow? How well does Django’s ORM perform?

Some developers might argue that an ORM is slow and will impact the performance of your application.

According to Andrew Aikman, it will only do so depending on how far the abstraction goes and how well your developers have mapped your object model maps to the database. Aikman notes:

  • Django’s ORM is lazy and performs queries on demand, this results in an N+1 selects issue where the database is queried for each item in a loop.
  • The solution to this issue is too eager to load using the built-in query optimisers select related and prefetch related.

In summary, stakeholders need to decide if they are willing to spend more developer time, using straight data access to speed up performance. If resources are tight, it’s worth compromising on a slower runtime for the technical debt that would come from a hard to maintain implementation.

 

Is Django’s ORM faster than SQL?

This is a popular question; we’ve seen indexed by Google based on search volumes. Most developers would agree that Raw SQL would be faster than ‘Eloquent ORM’. However, how much faster is difficult to quantify because there are inherent trade-offs between ease of development and performance.

 

Is Django ORM good?

Yes, and here’s why you use ORM most of the time…

According to one Reddit user, an ORM should be viewed as a storage layer. While an ORM uses SQL as its backend, you should only run SQL if your query can be written more clearly to run faster. For other scenarios, it’s best to stick to an ORM because it is more understandable by more people as opposed to anything you’re going to write by hand.

 

What databases does Django’s ORM officially support?

Django web framework includes an ORM layer to interact with application data from different relational databases, including:

Note: There are also a number of database backends provided by third parties.

 

Want to learn more?

Eliga Services has been working for many years with Django ORM. In fact, we’re a leading contributor to the open-source Django CMS project. Check us out on GitHub, where you’ll find Andrew Aikman (Aiky30) listed under Community Heroes.

If you’re a developer thinking of working with Django, it’s worth checking out the Django CMS community job board or these Django projects on GitHub.

Learn more about Django here.

Book a time to chat with us about Django CMS and what it can do for your business here.