Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Django
  • Views
  • Model
  • Template
  • Forms
  • Jinja
  • Python SQLite
  • Flask
  • Json
  • Postman
  • Interview Ques
  • MongoDB
  • Python MongoDB
  • Python Database
  • ReactJS
  • Vue.js
Open In App
Next Article:
Django QuerySet.values() for Single Object
Next article icon

Django QuerySets: Introduction and Using get()

Last Updated : 22 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A QuerySet is a collection of data retrieved from the database. You can think of it as a list of objects.
QuerySets make it easier to get only the data you need by letting you filter, sort, and organize your data early on before actually fetching it from the database.

In this article, we will learn how to get data from a model into a QuerySet using get method:

get() Method: Retrieve a Single Object

The get() method returns exactly one object that matches the conditions you provide. It is useful when you expect only one result.

If no object matches, Django raises a DoesNotExist exception. If more than one object matches, it raises a MultipleObjectsReturned exception.

Syntax

Model.objects.get(**kwargs)

  • Model is your Django model.
  • kwargs are the conditions used to find the object.

Example: Using get() with a Book Model

Suppose we have a Book model with fields like title and author. To get the book titled "The Great Gatsby":

Python
from app.models import Book  try:     book = Book.objects.get(title='The Great Gatsby')     # Use the book object here except Book.DoesNotExist:     print("No book found with that title.") except Book.MultipleObjectsReturned:     print("Multiple books found with that title.") 

Using Multiple Conditions (AND)

You can provide multiple filters to narrow down results. For example, suppose we have multiple books with title "The Great Gatsby", but we only need results for the book to be written by author "F. Scott Fitzgerald", then we provide multiple query conditions:

Python
try:     book = Book.objects.get(title='The Great Gatsby', author='F. Scott Fitzgerald') except Book.DoesNotExist:     print("No matching book found.") except Book.MultipleObjectsReturned:     print("More than one book matches your query.") 

Django treats these conditions as an AND operation, returning the object that matches all conditions.

Using Primary Key with get()

Fetching objects by their primary key (usually id) is common:

book = Book.objects.get(pk=1) # Get book with primary key 1

Practical Uses of get()

  • Authenticating users by username or email.
  • Retrieving records using unique fields like primary keys.
  • Getting exact matches when you expect one record.

Using get() in the Django Shell

The Django shell is an interactive Python environment that lets you experiment with your Django models and QuerySets in real-time. It’s a great tool for testing database queries, exploring your data, and debugging without needing to write views or templates first.

Example: Fetching a Book Object Using get()

Consider the book model we created earlier and we need to fetch data from it using Python Shell, here's how we can do it:

1. Activate the Shell:

python manage.py shell

2. Use the following commands:

>>> from app.models import Book

>>> book = Book.objects.get(id=2)

>>> print(book)

This should print the details of book with id = 2.

django26
Snapshot of the terminal shell

Note: If no book with id=3 exists, a DoesNotExist exception will be raised; if multiple entries matched (unlikely with primary keys), a MultipleObjectsReturned exception would occur.


Next Article
Django QuerySet.values() for Single Object

A

amitgupm1wy
Improve
Article Tags :
  • Python
  • Geeks Premier League
  • Python Django
  • Geeks Premier League 2023
Practice Tags :
  • python

Similar Reads

    Count() vs len() on a Django QuerySet
    In Django, when working with database query sets, developers often need to determine the number of records that meet certain criteria. Django offers two primary ways to accomplish this: using the count() method on a QuerySet, or the Python built-in len() function. Each method has its specific use ca
    3 min read
    Extracting SQL Queries from Django QuerySets
    Django, a high-level Python web framework, simplifies the development of secure and maintainable websites. One of its powerful features is the Object-Relational Mapping (ORM) system, which allows developers to interact with the database using Python code instead of raw SQL. However, there are times
    3 min read
    Django Query Set - get
    A QuerySet is a collection of data retrieved from the database. You can think of it as a list of objects. QuerySets make it easier to get only the data you need by letting you filter, sort, and organize your data early on before actually fetching it from the database.In this article, we will learn h
    3 min read
    Django QuerySet.values() for Single Object
    In Django, QuerySet.Values() method helps us get specific fields from the database as dictionaries, making our queries simpler. This can be incredibly useful when we only need certain pieces of information from our model without the overhead of loading the full object.What is QuerySet.values()?The v
    3 min read
    How to combine multiple QuerySets in Django?
    QuerySets allow you to filter, order, and manipulate data from your database using a high-level Pythonic syntax. However, there are situations where you may need to combine multiple QuerySets into a single QuerySet to work with the data more efficiently. This article will explore various methods to
    5 min read
    How to Output Django QuerySet as JSON
    In Django, a common task for web developers is to return data in JSON format, especially when working with APIs. A Django QuerySet, which is a collection of database queries, can be serialized and output as JSON. This article will guide us through how to output a Django QuerySet as JSON using two me
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences