1
0
mirror of https://github.com/django/django.git synced 2025-10-24 14:16:09 +00:00

[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 6ed6a18a0 from master.
This commit is contained in:
Claude Paroz
2012-12-12 20:34:59 +01:00
parent 8a1b4dbda2
commit c20c3a20a3
2 changed files with 34 additions and 1 deletions

View File

@@ -80,3 +80,28 @@ class GetObjectOr404Tests(TestCase):
get_list_or_404(Article.objects.all(), title__icontains="Run"),
[article]
)
def test_bad_class(self):
# Given an argument klass that is not a Model, Manager, or Queryset
# raises a helpful ValueError message
self.assertRaisesMessage(ValueError,
"Object is of type 'str', but must be a Django Model, Manager, "
"or QuerySet",
get_object_or_404, "Article", title__icontains="Run"
)
class CustomClass(object):
pass
self.assertRaisesMessage(ValueError,
"Object is of type 'CustomClass', but must be a Django Model, "
"Manager, or QuerySet",
get_object_or_404, CustomClass, title__icontains="Run"
)
# Works for lists too
self.assertRaisesMessage(ValueError,
"Object is of type 'list', but must be a Django Model, Manager, "
"or QuerySet",
get_list_or_404, [Article], title__icontains="Run"
)