The preceding section discussed how to add objects to the database. This section discusses how to retrieve those objects from the database. The Django database-abstraction API provides several methods for retrieving objects from the SQL database. Like the save() function, these methods are automatically built into each class you define in your models.py file.
The database-abstraction layer provides an object manager that gives you access to objects in the database. Each model you define will have at least one manager called objects. For example, if you have a model called Blog, you can access the objects manager using Blog.objects.
The following sections describe two of the more common ways to retrieve objects from the database using the object manager objects. We will cover more complex queries later in this hour.
The simplest way to retrieve objects from the SQL database is to use the all() function that is built into the object manager in each model. The all() function returns a QuerySet that represents a collection of objects from the database. For example, to retrieve all Blog entries from the database, you would use the following code:
from iFriends.People.models import Blog blogs = Blog.objects.all()
A QuerySet is basically a Python array that can be indexed, iterated in for loops, and sliced. For example, to iterate through all the entries in Blog and print the title, you would use the following code:
from iFriends.People.models import Blog blogs = Blog.objects.all() for b in blogs: print b.title
By the Way
QuerySets do not actually access the database until they are evaluated in some way. In the preceding example, the database does not actually get accessed until b.title is accessed. This is useful, because the entire contents of the database do not need to be read in at first.
You can also use the Python len() and list() functions on the QuerySet. For example, to print the title of the last entry in Blog, you would use the following code to evaluate the number of entries and then index the last entry:
from iFriends.People.models import Blog blogs = Blog.objects.all() blog_list = list(blogs) b = blogs[len(blogs)-1] print b.title
Watch Out!
When you use the list() function to convert the QuerySet into a Python list, all objects are read into memory from the database and are added as entries in the list. If the database is large, it can take a large amount of memory and make the list difficult to manage.
The preceding section showed you how to retrieve all objects from the database. In this section, you will learn how to retrieve only one object from the database using the get() function. The get() function does not return a QuerySet, as the all() function does. Instead, the get() function returns a single object if it is found. For example, to print the title field of the Blog entry with an id of 1, you would use the following code:
from iFriends.People.models import Blog b = Blog.objects.get(id=1) print b.title
The get() function accepts a list of arguments that can be any number of the fields in the object's model. For example, if you want to get the Blog object with a specific value in the title field, you would use the following code:
from iFriends.People.models import Blog b = Blog.objects.get(title='MyBlog') print b.title
The most useful feature of the get() function is that it raises exceptions if no object or multiple objects are found. If the get() function does not find an object, a DoesNotExist exception is raised. If more than one entry is found, an AssertionError exception is raised. These exceptions are useful if you need to be able to add code to handle conditions where the specific objects were not found. For example, the following code snippet handles conditions when the object wasn't found:
from iFriends.People.models import Blog try: b = Blog.objects.get(title='MyBlog') print b.title except Blog.DoesNotExist: print 'Object Not Found'
Did you Know?
The DoesNotExist exception inherits from the django.core.exceptions.ObjectDoesNotExist class. You can import that class and then use ObjectDoesNotExist instead of DoesNotExist so that you can handle all ObjectDoesNotExist types of exceptions.
Try It Yourself: Retrieve Objects from the DatabaseIn this section, you will apply the all() and get() functions to retrieve Person objects from the People table in the SQL database. Follow these steps in the Django shell to access the object manager for the People class, and use it to retrieve objects from the database:
|