mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-14 05:36:50 +03:00
Merge pull request #683 from dbrgn/serializer_exception_msg
Improved exception message for missing serializer model meta option
This commit is contained in:
commit
d59ed64915
|
@ -424,6 +424,8 @@ class ModelSerializer(Serializer):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
cls = self.opts.model
|
cls = self.opts.model
|
||||||
|
assert cls is not None, \
|
||||||
|
"Serializer class '%s' is missing 'model' Meta option" % self.__class__.__name__
|
||||||
opts = get_concrete_model(cls)._meta
|
opts = get_concrete_model(cls)._meta
|
||||||
pk_field = opts.pk
|
pk_field = opts.pk
|
||||||
# while pk_field.rel:
|
# while pk_field.rel:
|
||||||
|
|
|
@ -91,6 +91,11 @@ class PositiveIntegerAsChoiceSerializer(serializers.ModelSerializer):
|
||||||
fields = ['some_integer']
|
fields = ['some_integer']
|
||||||
|
|
||||||
|
|
||||||
|
class BrokenModelSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
fields = ['some_field']
|
||||||
|
|
||||||
|
|
||||||
class BasicTests(TestCase):
|
class BasicTests(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.comment = Comment(
|
self.comment = Comment(
|
||||||
|
@ -353,6 +358,18 @@ class ValidationTests(TestCase):
|
||||||
|
|
||||||
self.assertIn('created', serializer.errors)
|
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 AssertionError as e:
|
||||||
|
self.assertEquals(e.args[0], "Serializer class 'BrokenModelSerializer' is missing 'model' Meta option")
|
||||||
|
except:
|
||||||
|
self.fail('Wrong exception type thrown.')
|
||||||
|
|
||||||
|
|
||||||
class CustomValidationTests(TestCase):
|
class CustomValidationTests(TestCase):
|
||||||
class CommentSerializerWithFieldValidator(CommentSerializer):
|
class CommentSerializerWithFieldValidator(CommentSerializer):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user