Queries are created using the Django model’s manager, typically accessed through the objects
attribute. This manager provides methods to retrieve and filter model instances.
Suppose you have an Author
model and want to retrieve all authors:
class Author(models.Model):
name = models.CharField(max_length=100)
birth_date = models.DateField()
# Retrieve all authors
authors = Author.objects.all()
Author.objects.all()
returns a QuerySet
containing all Author
instances.You can filter queries to retrieve specific subsets of data using the filter()
method, which accepts keyword arguments corresponding to model fields.
Retrieve authors born after January 1, 2000:
# Filter authors by birth_date
recent_authors = Author.objects.filter(birth_date__gt='2000-01-01')
birth_date__gt='2000-01-01'
filters authors with birth_date
greater than January 1, 2000.Django allows chaining multiple query methods to refine your queries further.
Retrieve authors with a specific name and birth date after January 1, 2000:
# Chain filter methods
specific_authors = Author.objects.filter(name='John').filter(birth_date__gt='2000-01-01')
filter()
methods to combine conditions.To retrieve a single object, use the get()
method. Note that get()
raises a DoesNotExist
exception if no objects match the query and a MultipleObjectsReturned
exception if more than one object matches.
Retrieve an author by name:
# Get a single author
author = Author.objects.get(name='John')
Author.objects.get(name='John')
retrieves the author with the name ‘John’.Use the exclude()
method to exclude certain objects from the result set.
Retrieve authors not born after January 1, 2000:
# Exclude authors by birth_date
non_recent_authors = Author.objects.exclude(birth_date__gt='2000-01-01')
exclude(birth_date__gt='2000-01-01')
excludes authors with birth_date
greater than January 1, 2000.