From 3e6f99e273b3ca4278c417f32f0db1b7bdfe408b Mon Sep 17 00:00:00 2001 From: Danilo Bargen Date: Mon, 25 Feb 2013 22:34:05 +0100 Subject: [PATCH 1/3] Improved exception message for missing serializer model meta option --- rest_framework/serializers.py | 2 ++ rest_framework/tests/serializer.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 266a24029..669e5ae95 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -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: diff --git a/rest_framework/tests/serializer.py b/rest_framework/tests/serializer.py index 671494b5d..af84c46b3 100644 --- a/rest_framework/tests/serializer.py +++ b/rest_framework/tests/serializer.py @@ -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): From ec4d79bcaf08d577e8c44cad873515d018e21986 Mon Sep 17 00:00:00 2001 From: Danilo Bargen Date: Mon, 25 Feb 2013 23:02:42 +0100 Subject: [PATCH 2/3] Show class name in exception message --- rest_framework/serializers.py | 3 ++- rest_framework/tests/serializer.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 669e5ae95..d57417d92 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -425,7 +425,8 @@ class ModelSerializer(Serializer): cls = self.opts.model if cls is None: - raise AttributeError("Serializer class is missing 'model' Meta option") + raise AttributeError("Serializer class '%s' is missing 'model' Meta option" % + self.__class__.__name__) opts = get_concrete_model(cls)._meta pk_field = opts.pk # while pk_field.rel: diff --git a/rest_framework/tests/serializer.py b/rest_framework/tests/serializer.py index af84c46b3..d4e9cc132 100644 --- a/rest_framework/tests/serializer.py +++ b/rest_framework/tests/serializer.py @@ -366,7 +366,7 @@ class ValidationTests(TestCase): try: serializer = BrokenModelSerializer() except AttributeError as e: - self.assertEquals(e.args[0], "Serializer class is missing 'model' Meta option") + self.assertEquals(e.args[0], "Serializer class 'BrokenModelSerializer' is missing 'model' Meta option") except: self.fail('Wrong exception type thrown.') From e476dcb8c70f3792f01214f8d4a13c9316e11341 Mon Sep 17 00:00:00 2001 From: Danilo Bargen Date: Tue, 26 Feb 2013 09:38:57 +0100 Subject: [PATCH 3/3] Changed AttributeError to AssertionError --- rest_framework/serializers.py | 5 ++--- rest_framework/tests/serializer.py | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index d57417d92..91af2af2d 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -424,9 +424,8 @@ class ModelSerializer(Serializer): """ cls = self.opts.model - if cls is None: - raise AttributeError("Serializer class '%s' is missing 'model' Meta option" % - self.__class__.__name__) + assert cls is not None, \ + "Serializer class '%s' is missing 'model' Meta option" % self.__class__.__name__ opts = get_concrete_model(cls)._meta pk_field = opts.pk # while pk_field.rel: diff --git a/rest_framework/tests/serializer.py b/rest_framework/tests/serializer.py index d4e9cc132..36703fd9c 100644 --- a/rest_framework/tests/serializer.py +++ b/rest_framework/tests/serializer.py @@ -365,7 +365,7 @@ class ValidationTests(TestCase): """ try: serializer = BrokenModelSerializer() - except AttributeError as e: + except AssertionError as e: self.assertEquals(e.args[0], "Serializer class 'BrokenModelSerializer' is missing 'model' Meta option") except: self.fail('Wrong exception type thrown.')