mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-29 17:39:48 +03:00
Handle missing Meta properly
This commit is contained in:
parent
2f5186be87
commit
edc05d0896
|
@ -862,15 +862,13 @@ class ModelSerializerMetaclass(SerializerMetaclass):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _get_fields(cls, new_class):
|
def _get_fields(cls, new_class):
|
||||||
# XXX
|
# ModelSerializer itself and customized subclasses like
|
||||||
|
# HyperlinkedModelSerializer are never instantiated directly,
|
||||||
|
# and don't have a Meta. For these, the base_fields is None,
|
||||||
|
# and an error is raised if it's attempted to be used.
|
||||||
if not hasattr(new_class, 'Meta'):
|
if not hasattr(new_class, 'Meta'):
|
||||||
return OrderedDict()
|
return None
|
||||||
|
|
||||||
assert hasattr(new_class, 'Meta'), (
|
|
||||||
'Class {serializer_class} missing "Meta" attribute'.format(
|
|
||||||
serializer_class=new_class.__name__
|
|
||||||
)
|
|
||||||
)
|
|
||||||
assert hasattr(new_class.Meta, 'model'), (
|
assert hasattr(new_class.Meta, 'model'), (
|
||||||
'Class {serializer_class} missing "Meta.model" attribute'.format(
|
'Class {serializer_class} missing "Meta.model" attribute'.format(
|
||||||
serializer_class=new_class.__name__
|
serializer_class=new_class.__name__
|
||||||
|
@ -1097,6 +1095,11 @@ class ModelSerializer(Serializer, metaclass=ModelSerializerMetaclass):
|
||||||
Return the dict of field names -> field instances that should be
|
Return the dict of field names -> field instances that should be
|
||||||
used for `self.fields` when instantiating the serializer.
|
used for `self.fields` when instantiating the serializer.
|
||||||
"""
|
"""
|
||||||
|
assert self.base_fields is not None, (
|
||||||
|
'Class {serializer_class} missing "Meta" attribute'.format(
|
||||||
|
serializer_class=self.__class__.__name__
|
||||||
|
)
|
||||||
|
)
|
||||||
return copy.deepcopy(self.base_fields)
|
return copy.deepcopy(self.base_fields)
|
||||||
|
|
||||||
# Methods for determining the set of field names to include...
|
# Methods for determining the set of field names to include...
|
||||||
|
|
|
@ -151,6 +151,14 @@ class TestModelSerializer(TestCase):
|
||||||
model = AbstractModel
|
model = AbstractModel
|
||||||
fields = ('afield',)
|
fields = ('afield',)
|
||||||
|
|
||||||
|
def test_missing_meta(self):
|
||||||
|
class TestSerializer(serializers.ModelSerializer):
|
||||||
|
text = serializers.CharField()
|
||||||
|
|
||||||
|
msginitial = 'Class TestSerializer missing "Meta" attribute'
|
||||||
|
with self.assertRaisesMessage(AssertionError, msginitial):
|
||||||
|
TestSerializer().fields
|
||||||
|
|
||||||
|
|
||||||
class TestRegularFieldMappings(TestCase):
|
class TestRegularFieldMappings(TestCase):
|
||||||
def test_regular_fields(self):
|
def test_regular_fields(self):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user