Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.5.x] Fixed #19432 -- Provided better error message for get_object_…

…or_404

Thanks Kit Sunde for the report and Brian Holdefehr for the initial
patch.
Backport of 6ed6a18 from master.
  • Loading branch information...
commit c20c3a20a313b24df6d115e585c04c8dc60d3970 1 parent 8a1b4db
Claude Paroz authored December 12, 2012
10  django/shortcuts/__init__.py
@@ -7,6 +7,7 @@
7 7
 from django.template import loader, RequestContext
8 8
 from django.http import HttpResponse, Http404
9 9
 from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect
  10
+from django.db.models.base import ModelBase
10 11
 from django.db.models.manager import Manager
11 12
 from django.db.models.query import QuerySet
12 13
 from django.core import urlresolvers
@@ -72,13 +73,20 @@ def _get_queryset(klass):
72 73
     """
73 74
     Returns a QuerySet from a Model, Manager, or QuerySet. Created to make
74 75
     get_object_or_404 and get_list_or_404 more DRY.
  76
+
  77
+    Raises a ValueError if klass is not a Model, Manager, or QuerySet.
75 78
     """
76 79
     if isinstance(klass, QuerySet):
77 80
         return klass
78 81
     elif isinstance(klass, Manager):
79 82
         manager = klass
80  
-    else:
  83
+    elif isinstance(klass, ModelBase):
81 84
         manager = klass._default_manager
  85
+    else:
  86
+        klass__name = klass.__name__ if isinstance(klass, type) \
  87
+                      else klass.__class__.__name__
  88
+        raise ValueError("Object is of type '%s', but must be a Django Model, "
  89
+                         "Manager, or QuerySet" % klass__name)
82 90
     return manager.all()
83 91
 
84 92
 def get_object_or_404(klass, *args, **kwargs):
25  tests/modeltests/get_object_or_404/tests.py
@@ -80,3 +80,28 @@ def test_get_object_or_404(self):
80 80
             get_list_or_404(Article.objects.all(), title__icontains="Run"),
81 81
             [article]
82 82
         )
  83
+
  84
+    def test_bad_class(self):
  85
+        # Given an argument klass that is not a Model, Manager, or Queryset
  86
+        # raises a helpful ValueError message
  87
+        self.assertRaisesMessage(ValueError,
  88
+            "Object is of type 'str', but must be a Django Model, Manager, "
  89
+            "or QuerySet",
  90
+            get_object_or_404, "Article", title__icontains="Run"
  91
+        )
  92
+
  93
+        class CustomClass(object):
  94
+            pass
  95
+
  96
+        self.assertRaisesMessage(ValueError,
  97
+            "Object is of type 'CustomClass', but must be a Django Model, "
  98
+            "Manager, or QuerySet",
  99
+            get_object_or_404, CustomClass, title__icontains="Run"
  100
+        )
  101
+
  102
+        # Works for lists too
  103
+        self.assertRaisesMessage(ValueError,
  104
+            "Object is of type 'list', but must be a Django Model, Manager, "
  105
+            "or QuerySet",
  106
+            get_list_or_404, [Article], title__icontains="Run"
  107
+        )

0 notes on commit c20c3a2

Please sign in to comment.
Something went wrong with that request. Please try again.