Allow redundant SerializerMethodField method names (#6767)

This commit is contained in:
Ryan P Kilby 2019-07-01 05:22:04 -07:00 committed by Tom Christie
parent 42fd179d4e
commit 91ea138406
2 changed files with 4 additions and 14 deletions

View File

@ -1835,12 +1835,6 @@ class SerializerMethodField(Field):
# 'method_name' argument has been used. For example:
# my_field = serializer.SerializerMethodField(method_name='get_my_field')
default_method_name = 'get_{field_name}'.format(field_name=field_name)
assert self.method_name != default_method_name, (
"It is redundant to specify `%s` on SerializerMethodField '%s' in "
"serializer '%s', because it is the same as the default method name. "
"Remove the `method_name` argument." %
(self.method_name, field_name, parent.__class__.__name__)
)
# The method name should default to `get_{field_name}`.
if self.method_name is None:

View File

@ -2212,17 +2212,13 @@ class TestSerializerMethodField:
}
def test_redundant_method_name(self):
# Prior to v3.10, redundant method names were not allowed.
# This restriction has since been removed.
class ExampleSerializer(serializers.Serializer):
example_field = serializers.SerializerMethodField('get_example_field')
with pytest.raises(AssertionError) as exc_info:
ExampleSerializer().fields
assert str(exc_info.value) == (
"It is redundant to specify `get_example_field` on "
"SerializerMethodField 'example_field' in serializer "
"'ExampleSerializer', because it is the same as the default "
"method name. Remove the `method_name` argument."
)
field = ExampleSerializer().fields['example_field']
assert field.method_name == 'get_example_field'
class TestValidationErrorCode: