Skip to content

Commit

Permalink
Assert declared fields are not present in exclude
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan P Kilby committed Nov 15, 2017
1 parent 33d3bd9 commit 3578ba6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
14 changes: 11 additions & 3 deletions rest_framework/serializers.py
Expand Up @@ -1099,12 +1099,20 @@ def get_field_names(self, declared_fields, info):
# Use the default set of field names if `Meta.fields` is not specified.
fields = self.get_default_field_names(declared_fields, info)

# deduplicate field names (occurs when a declared fields matches a model field)
fields = list(OrderedDict.fromkeys(fields))

if exclude is not None:
# If `Meta.exclude` is included, then remove those fields.
for field_name in exclude:
assert field_name not in self._declared_fields, (
"Cannot both declare the field '{field_name}' and include "
"it in the {serializer_class} 'exclude' option. Remove the "
"field or, if inherited from a parent serializer, disable "
"with `{field_name} = None`."
.format(
field_name=field_name,
serializer_class=self.__class__.__name__
)
)

assert field_name in fields, (
"The field '{field_name}' was included on serializer "
"{serializer_class} in the 'exclude' option, but does "
Expand Down
8 changes: 7 additions & 1 deletion tests/test_model_serializer.py
Expand Up @@ -908,7 +908,13 @@ class Meta:
model = MetaClassTestModel
exclude = ('text',)

assert list(ExampleSerializer().fields) == ['id']
expected = (
"Cannot both declare the field 'text' and include it in the "
"ExampleSerializer 'exclude' option. Remove the field or, if "
"inherited from a parent serializer, disable with `text = None`."
)
with self.assertRaisesMessage(AssertionError, expected):
ExampleSerializer().fields


class Issue2704TestCase(TestCase):
Expand Down

0 comments on commit 3578ba6

Please sign in to comment.