summaryrefslogtreecommitdiff
path: root/django
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 /django
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 'django')
-rw-r--r--django/shortcuts/__init__.py41
1 files changed, 24 insertions, 17 deletions
diff --git a/django/shortcuts/__init__.py b/django/shortcuts/__init__.py
index 9d7120bc7a..e4bba0919e 100644
--- a/django/shortcuts/__init__.py
+++ b/django/shortcuts/__init__.py
@@ -7,6 +7,7 @@ for convenience's sake.
from django.template import loader
from django.http import HttpResponse, Http404
from django.db.models.manager import Manager
+from django.db.models.query import QuerySet
def render_to_response(*args, **kwargs):
"""
@@ -16,40 +17,46 @@ def render_to_response(*args, **kwargs):
return HttpResponse(loader.render_to_string(*args, **kwargs))
load_and_render = render_to_response # For backwards compatibility.
+def _get_queryset(klass):
+ """
+ Return a QuerySet from a Model, Manager, or QuerySet. Created to make
+ get_object_or_404 and get_list_or_404 more DRY.
+ """
+ if isinstance(klass, QuerySet):
+ return klass
+ elif isinstance(klass, Manager):
+ manager = klass
+ else:
+ manager = klass._default_manager
+ return manager.all()
+
def get_object_or_404(klass, *args, **kwargs):
"""
Use get() to return an object, or raise a Http404 exception if the object
does not exist.
- klass may be a Model or Manager object. All other passed
+ klass may be a Model, Manager, or QuerySet object. All other passed
arguments and keyword arguments are used in the get() query.
Note: Like with get(), an AssertionError will be raised if more than one
object is found.
"""
- if isinstance(klass, Manager):
- manager = klass
- klass = manager.model
- else:
- manager = klass._default_manager
+ queryset = _get_queryset(klass)
try:
- return manager.get(*args, **kwargs)
- except klass.DoesNotExist:
- raise Http404('No %s matches the given query.' % klass._meta.object_name)
+ return queryset.get(*args, **kwargs)
+ except queryset.model.DoesNotExist:
+ raise Http404('No %s matches the given query.' % queryset.model._meta.object_name)
def get_list_or_404(klass, *args, **kwargs):
"""
Use filter() to return a list of objects, or raise a Http404 exception if
- the list is empty.
+ the list is emtpy.
- klass may be a Model or Manager object. All other passed
+ klass may be a Model, Manager, or QuerySet object. All other passed
arguments and keyword arguments are used in the filter() query.
"""
- if isinstance(klass, Manager):
- manager = klass
- else:
- manager = klass._default_manager
- obj_list = list(manager.filter(*args, **kwargs))
+ queryset = _get_queryset(klass)
+ obj_list = list(queryset.filter(*args, **kwargs))
if not obj_list:
- raise Http404('No %s matches the given query.' % manager.model._meta.object_name)
+ raise Http404('No %s matches the given query.' % queryset.model._meta.object_name)
return obj_list