Code-Memo

Creating Queries

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.

Simple Query

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()

Filtering Queries

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')

Chaining Query Methods

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')

Retrieving a Single Object

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')

Excluding Objects from QuerySets

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')