What is an ORM?

In short
An ORM, or object-relational mapper, is a library that lets you work with database rows as ordinary objects in your programming language. You describe your data as classes, and the ORM creates the tables, saves objects, and reads them back for you, so you write less raw SQL.
The problem an ORM solves
Most applications keep their data in a relational database. That database stores information in tables made of rows and columns. Your program, however, works with objects, such as a user with a name and an email address. Translating between these two worlds by hand means writing many small SQL statements and copying values back and forth. An object-relational mapper, usually shortened to ORM, does this translation for you.
How it works
You define a class in your code for each kind of data you store. Each attribute of the class describes one piece of data, and the ORM maps it to a column in a database table. Each class usually maps to one table, and each object you create from the class maps to one row.
The Django web framework, for example, uses this approach. The official documentation describes a model as the single, definitive source of information about your data. A model is a Python class, and each attribute of that class represents a database field. Here is a small example:
from django.db import models
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
From this class, the ORM can create a table for people, with an id column added automatically and one column for each name. The exact SQL depends on the database you use. The ORM handles the differences, so the same class can work with several database engines.
Once the model exists, you use it through objects rather than SQL. You create a person, change a field, and save it. The ORM turns those actions into the right database statements behind the scenes.
Fields and their options
Fields are the most important part of a model. Each field is a class attribute and has a type. The type tells the database what kind of data to store, and it also affects how forms display the field and how the data is checked. Options let you adjust a field further. Two common ones are null and blank. The documentation explains that null is about how the database stores an empty value, while blank is about whether a form accepts an empty value. They are easy to confuse, so it helps to remember that one is for the database and the other is for validation.
A field can also be marked as the primary key, which is the value that identifies each row. If you do not choose one, the ORM adds an automatically incrementing id field for you.
When to use an ORM
An ORM is a good fit when your application has many similar records, when you want the same code to run on different databases, or when you want to avoid writing repeated SQL. It also helps with schema changes, because most ORMs can record changes to tables as migrations that you apply in order.
An ORM is less helpful when you need very specific queries that the ORM cannot express well, or when performance depends on fine control over the SQL. In those cases, many ORMs let you drop down to raw SQL for part of the work.
Common mistakes
Beginners often forget to register their app with the framework before running migrations, so the tables are never created. Another frequent mistake is choosing field names that clash with methods the model already has, such as save or delete. Finally, some developers load too many related objects at once, which can slow an application down. Reading the generated queries, at least occasionally, helps you spot these problems early.
Alternatives
You can always write SQL by hand and send it through a database driver. This gives you full control but more work. Some developers prefer a query builder, which helps you assemble SQL in code without mapping rows to objects. Choosing between these options depends on how much structure you want and how complex your queries are.
FAQ
Do I still need to know SQL if I use an ORM?
Yes, it helps. An ORM writes SQL for you, but understanding what tables and queries look like makes it easier to debug slow or incorrect code.
Is an ORM the same as a database?
No. An ORM sits between your code and the database. The database stores the data, while the ORM translates between objects in your program and rows in tables.
What is the difference between null and blank in a field?
The null option controls whether the database stores an empty value as NULL. The blank option controls whether form validation accepts an empty value.
Why does my model get an id field automatically?
Most tables need a unique identifier for each row. If you do not mark a field as the primary key, the ORM adds an automatically incrementing id field for this purpose.
Sources
This page is based on the official documentation below. Read it for full detail.
Object-relational mapping — official documentation ↗