ORM (Django)
Django's ORM (Object-Relational Mapper) lets you work with the database using Python objects instead of SQL. You query, create, update, and delete rows through model classes, and Django translates it into SQL for your database.
Django
The ORM is one of Django's defining features. Instead of writing SELECT * FROM post WHERE published=true, you write Post.objects.filter(published=True). The ORM generates the SQL, runs it, and returns model instances, abstracting away database differences between SQLite, PostgreSQL, MySQL, and others.
You reach the ORM through each model's objects manager, which returns QuerySets you can filter, order, and slice. Creating is Post.objects.create(...) or instance.save(); deleting is instance.delete(). Relationships are traversed with attribute access (post.author) and reverse accessors (author.post_set).
The ORM is convenient but not free: naive code can trigger the N+1 query problem when looping over related objects. select_related and prefetch_related fix that, and you can drop to raw SQL when needed.
Syntax
Post.objects.filter(published=True).order_by("-created")Examples
Python
# create
Post.objects.create(title="Hi", body="...")
# read
recent = Post.objects.filter(published=True).order_by("-created")[:5]
# update
Post.objects.filter(pk=1).update(title="Edited")
# delete
Post.objects.filter(pk=2).delete()