Improved exception message for missing serializer model meta option

This commit is contained in:
Danilo Bargen 2013-02-25 22:34:05 +01:00
parent 8da83f0df9
commit 3e6f99e273
2 changed files with 19 additions and 0 deletions

View File

@ -424,6 +424,8 @@ class ModelSerializer(Serializer):
"""
cls = self.opts.model
if cls is None:
raise AttributeError("Serializer class is missing 'model' Meta option")
opts = get_concrete_model(cls)._meta
pk_field = opts.pk
# while pk_field.rel:

View File

@ -91,6 +91,11 @@ class PositiveIntegerAsChoiceSerializer(serializers.ModelSerializer):
fields = ['some_integer']
class BrokenModelSerializer(serializers.ModelSerializer):
class Meta:
fields = ['some_field']
class BasicTests(TestCase):
def setUp(self):
self.comment = Comment(
@ -353,6 +358,18 @@ class ValidationTests(TestCase):
self.assertIn('created', serializer.errors)
def test_missing_model_field_exception_msg(self):
"""
Assert that a meaningful exception message is outputted when the model
field is missing (e.g. when mistyping ``model``).
"""
try:
serializer = BrokenModelSerializer()
except AttributeError as e:
self.assertEquals(e.args[0], "Serializer class is missing 'model' Meta option")
except:
self.fail('Wrong exception type thrown.')
class CustomValidationTests(TestCase):
class CommentSerializerWithFieldValidator(CommentSerializer):