Django ORM Basics
# CHAPTER 9
Django ORM Basics
1. Introduction
In the previous chapter, we used the Django ORM to *create* the database tables. In this chapter, we will use the ORM to *interact* with those tables. This means performing CRUD operations: Create, Read, Update, and Delete. Instead of writing raw SQL commands (SELECT * FROM blog_post WHERE id=1), we will use elegant, native Python methods. Mastering the ORM is arguably the most important skill for a Django backend developer.
2. Learning Objectives
By the end of this chapter, you will be able to:- Use the Django Interactive Shell to test database queries.
-
Create new records using
.save()and.create().
-
Retrieve all records using
.objects.all().
-
Filter and retrieve specific records using
.objects.filter()and.objects.get().
- Update and Delete existing records.
3. Beginner-Friendly Explanation
Imagine interacting with a massive corporate filing cabinet. If you use raw SQL, you have to speak a specialized, complex language to the filing clerk: "SELECT document FROM cabinet WHERE color='blue' AND year=2026." If you use the Django ORM, you simply speak plain English (Python) to an assistant: "Assistant,Post.objects.filter(color='blue')." The assistant translates it to SQL, runs to the cabinet, and brings back a neat Python list of files. You never need to learn the complex SQL language.
4. Step 1: The Django Interactive Shell
To learn the ORM, we won't write code inviews.py just yet. We will use the Django Shell, a powerful terminal tool that lets us execute Python code live against our database.
Open your terminal and run:
*Your terminal prompt will change to >>>. You are now writing live Python code!*
5. Step 2: CREATE Operations
Let's add some blog posts to the database. First, import the Model.6. Step 3: READ Operations
Let's query the database to fetch our data.Get all records:
*(A QuerySet is simply a Python list of objects).*
Get a single specific record (by ID):
Filter records based on a condition:
7. Step 4: UPDATE Operations
To update a record, you fetch it, change the Python attribute, and call.save().
8. Step 5: DELETE Operations
To delete a record, you fetch it and call.delete().
*(To exit the interactive shell, type exit() and press Enter).*
9. Backend Workflow: Field Lookups (Advanced Filtering)
The true power of the ORM is "Field Lookups". By adding double underscores__ to a field name, you can execute complex SQL WHERE clauses.
-
Post.objects.filter(title__contains='Django')(SQL: LIKE '%Django%')
-
Post.objects.filter(title__startswith='Updated')
-
Post.objects.filter(id__gt=5)(id Greater Than 5)
10. Best Practices
-
.get()vs.filter(): This is a crucial distinction.
-
Use
.get()when you expect EXACTLY ONE unique record (like an ID or an Email). If it fails to find the record, it throws aDoesNotExisterror.
-
Use
.filter()when you are searching for a category (like finding all posts written in 2026). If it finds nothing, it safely returns an empty list[].
11. Common Mistakes
-
Forgetting
.save(): A classic beginner mistake is typingpost = Post(title="New")and wondering why the data isn't in the database. ThePost()command only creates the object in your laptop's temporary RAM. It is not permanent until you executepost.save().
12. Exercises
-
1.
What is the fundamental difference between the output of
Post.objects.all()andPost.objects.get(id=1)?
13. Coding Challenges
-
Challenge: Open the
python manage.py shell. Import your Post model. Write a query using.filter()and a Field Lookup (__icontains) to find all posts where the content contains the word "awesome".
14. MCQs with Answers
Which ORM method is used to retrieve a single, unique record from the database, and will throw an error if multiple records are found?
What is the correct syntax for an ORM Field Lookup to find all posts where the title contains the word "Django"?
15. Interview Questions
-
Q: Compare and contrast the
.get()and.filter()methods in the Django ORM. Provide a real-world scenario detailing when to use each.
-
Q: Explain the concept of "QuerySet Evaluation" (Lazy Loading). If you write
q = Post.objects.all(), does Django immediately execute the SQL query to the database?
16. FAQs
Q: Can I write raw SQL in Django if the ORM is too slow? A: Yes. For incredibly complex reporting queries, you can usePost.objects.raw('SELECT * FROM blog_post'). However, for 99% of web development, the ORM is heavily optimized and highly secure against SQL injection. Avoid raw SQL unless absolutely necessary.
17. Summary
In Chapter 9, we unlocked the ability to manipulate data. Utilizing the Django Interactive Shell, we executed Create, Read, Update, and Delete (CRUD) operations using native Python syntax. We differentiated between extracting single objects with.get() and extracting lists of objects using .filter(). Finally, we explored the powerful double-underscore __ syntax for advanced database lookups.