Update ModelSerializer fields behavior

This commit is contained in:
José Padilla 2015-08-27 13:09:08 -04:00
parent 8264222497
commit 654e0e4527
2 changed files with 34 additions and 2 deletions

View File

@ -12,6 +12,8 @@ response content is handled by parsers and renderers.
""" """
from __future__ import unicode_literals from __future__ import unicode_literals
import warnings
from django.db import models from django.db import models
from django.db.models.fields import Field as DjangoModelField from django.db.models.fields import Field as DjangoModelField
from django.db.models.fields import FieldDoesNotExist from django.db.models.fields import FieldDoesNotExist
@ -51,6 +53,8 @@ LIST_SERIALIZER_KWARGS = (
'instance', 'data', 'partial', 'context', 'allow_null' 'instance', 'data', 'partial', 'context', 'allow_null'
) )
ALL_FIELDS = '__all__'
# BaseSerializer # BaseSerializer
# -------------- # --------------
@ -943,13 +947,13 @@ class ModelSerializer(Serializer):
fields = getattr(self.Meta, 'fields', None) fields = getattr(self.Meta, 'fields', None)
exclude = getattr(self.Meta, 'exclude', None) exclude = getattr(self.Meta, 'exclude', None)
if fields and not isinstance(fields, (list, tuple)): if fields and fields != ALL_FIELDS and not isinstance(fields, (list, tuple)):
raise TypeError( raise TypeError(
'The `fields` option must be a list or tuple. Got %s.' % 'The `fields` option must be a list or tuple. Got %s.' %
type(fields).__name__ type(fields).__name__
) )
if exclude and not isinstance(exclude, (list, tuple)): if exclude and exclude != ALL_FIELDS and not isinstance(exclude, (list, tuple)):
raise TypeError( raise TypeError(
'The `exclude` option must be a list or tuple. Got %s.' % 'The `exclude` option must be a list or tuple. Got %s.' %
type(exclude).__name__ type(exclude).__name__
@ -962,6 +966,19 @@ class ModelSerializer(Serializer):
) )
) )
if fields is None and exclude is None:
warnings.warn(
"Creating a ModelSerializer without either the 'fields' attribute "
"or the 'exclude' attribute will be prohibited. "
"The {serializer_class} serializer needs updating.".format(
serializer_class=self.__class__.__name__
),
PendingDeprecationWarning
)
if fields == ALL_FIELDS:
fields = None
if fields is not None: if fields is not None:
# Ensure that all declared fields have also been included in the # Ensure that all declared fields have also been included in the
# `Meta.fields` option. # `Meta.fields` option.

View File

@ -321,6 +321,21 @@ class TestRegularFieldMappings(TestCase):
ExampleSerializer() ExampleSerializer()
def test_fields_and_exclude_behavior(self):
class ImplicitFieldsSerializer(serializers.ModelSerializer):
class Meta:
model = RegularFieldsModel
class ExplicitFieldsSerializer(serializers.ModelSerializer):
class Meta:
model = RegularFieldsModel
fields = '__all__'
implicit = ImplicitFieldsSerializer()
explicit = ExplicitFieldsSerializer()
assert implicit.data == explicit.data
@pytest.mark.skipif(django.VERSION < (1, 8), @pytest.mark.skipif(django.VERSION < (1, 8),
reason='DurationField is only available for django1.8+') reason='DurationField is only available for django1.8+')