summaryrefslogtreecommitdiff
path: root/tests/modeltests/get_object_or_404
diff options
context:
space:
mode:
authorGary Wilson Jr <gary.wilson@gmail.com>2007-07-22 03:41:11 +0000
committerGary Wilson Jr <gary.wilson@gmail.com>2007-07-22 03:41:11 +0000
commitcae92ae615b8622c25266463e392ef913066ea21 (patch)
tree82305bc6bd92af65ac71608615ae8321e0e796ed /tests/modeltests/get_object_or_404
parent63cc023eea4faa0af570b548375666e48af15904 (diff)
Fixed #4373 -- Modified the get_object_or_404/get_list_or_404 shortcuts to also accept `QuerySet`s. Thanks SuperJared.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5746 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/modeltests/get_object_or_404')
-rw-r--r--tests/modeltests/get_object_or_404/models.py27
1 files changed, 24 insertions, 3 deletions
diff --git a/tests/modeltests/get_object_or_404/models.py b/tests/modeltests/get_object_or_404/models.py
index 5f449f4cfe..573eaf2047 100644
--- a/tests/modeltests/get_object_or_404/models.py
+++ b/tests/modeltests/get_object_or_404/models.py
@@ -3,11 +3,11 @@
get_object_or_404 is a shortcut function to be used in view functions for
performing a get() lookup and raising a Http404 exception if a DoesNotExist
-exception was rasied during the get() call.
+exception was raised during the get() call.
get_list_or_404 is a shortcut function to be used in view functions for
performing a filter() lookup and raising a Http404 exception if a DoesNotExist
-exception was rasied during the filter() call.
+exception was raised during the filter() call.
"""
from django.db import models
@@ -69,11 +69,28 @@ Http404: No Article matches the given query.
>>> get_object_or_404(Article.by_a_sir, title="Run away!")
<Article: Run away!>
+# QuerySets can be used too.
+>>> get_object_or_404(Article.objects.all(), title__contains="Run")
+<Article: Run away!>
+
+# Just as when using a get() lookup, you will get an error if more than one
+# object is returned.
+>>> get_object_or_404(Author.objects.all())
+Traceback (most recent call last):
+...
+AssertionError: get() returned more than one Author -- it returned ...! Lookup parameters were {}
+
+# Using an EmptyQuerySet raises a Http404 error.
+>>> get_object_or_404(Article.objects.none(), title__contains="Run")
+Traceback (most recent call last):
+...
+Http404: No Article matches the given query.
+
# get_list_or_404 can be used to get lists of objects
>>> get_list_or_404(a.article_set, title__icontains='Run')
[<Article: Run away!>]
-# Http404 is returned if the list is empty
+# Http404 is returned if the list is empty.
>>> get_list_or_404(a.article_set, title__icontains='Shrubbery')
Traceback (most recent call last):
...
@@ -83,4 +100,8 @@ Http404: No Article matches the given query.
>>> get_list_or_404(Article.by_a_sir, title__icontains="Run")
[<Article: Run away!>]
+# QuerySets can be used too.
+>>> get_list_or_404(Article.objects.all(), title__icontains="Run")
+[<Article: Run away!>]
+
"""}