Use ImproperlyConfigured when model meta lookup fails

This commit is contained in:
Tom Christie 2014-11-28 15:36:04 +00:00
parent 6fbd23ab34
commit 3a5b3772fe
2 changed files with 6 additions and 4 deletions

View File

@ -6,6 +6,7 @@ relationships and their associated metadata.
Usage: `get_field_info(model)` returns a `FieldInfo` instance. Usage: `get_field_info(model)` returns a `FieldInfo` instance.
""" """
from collections import namedtuple from collections import namedtuple
from django.core.exceptions import ImproperlyConfigured
from django.db import models from django.db import models
from django.utils import six from django.utils import six
from rest_framework.compat import OrderedDict from rest_framework.compat import OrderedDict
@ -44,9 +45,9 @@ def _resolve_model(obj):
if isinstance(obj, six.string_types) and len(obj.split('.')) == 2: if isinstance(obj, six.string_types) and len(obj.split('.')) == 2:
app_name, model_name = obj.split('.') app_name, model_name = obj.split('.')
resolved_model = models.get_model(app_name, model_name) resolved_model = models.get_model(app_name, model_name)
if not resolved_model: if resolved_model is None:
raise ValueError("Django did not return a model for " msg = "Django did not return a model for {0}.{1}"
"{0}.{1}".format(app_name, model_name)) raise ImproperlyConfigured(msg.format(app_name, model_name))
return resolved_model return resolved_model
elif inspect.isclass(obj) and issubclass(obj, models.Model): elif inspect.isclass(obj) and issubclass(obj, models.Model):
return obj return obj

View File

@ -1,4 +1,5 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from django.core.exceptions import ImproperlyConfigured
from django.conf.urls import patterns, url from django.conf.urls import patterns, url
from django.test import TestCase from django.test import TestCase
from django.utils import six from django.utils import six
@ -161,5 +162,5 @@ class ResolveModelWithPatchedDjangoTests(TestCase):
rest_framework.utils.model_meta.models.get_model = self.get_model rest_framework.utils.model_meta.models.get_model = self.get_model
def test_blows_up_if_model_does_not_resolve(self): def test_blows_up_if_model_does_not_resolve(self):
with self.assertRaises(ValueError): with self.assertRaises(ImproperlyConfigured):
_resolve_model('tests.BasicModel') _resolve_model('tests.BasicModel')