mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 17:47:04 +03:00
Allow redundant SerializerMethodField method names (#6767)
This commit is contained in:
parent
42fd179d4e
commit
91ea138406
|
@ -1835,12 +1835,6 @@ class SerializerMethodField(Field):
|
||||||
# 'method_name' argument has been used. For example:
|
# 'method_name' argument has been used. For example:
|
||||||
# my_field = serializer.SerializerMethodField(method_name='get_my_field')
|
# my_field = serializer.SerializerMethodField(method_name='get_my_field')
|
||||||
default_method_name = 'get_{field_name}'.format(field_name=field_name)
|
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}`.
|
# The method name should default to `get_{field_name}`.
|
||||||
if self.method_name is None:
|
if self.method_name is None:
|
||||||
|
|
|
@ -2212,17 +2212,13 @@ class TestSerializerMethodField:
|
||||||
}
|
}
|
||||||
|
|
||||||
def test_redundant_method_name(self):
|
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):
|
class ExampleSerializer(serializers.Serializer):
|
||||||
example_field = serializers.SerializerMethodField('get_example_field')
|
example_field = serializers.SerializerMethodField('get_example_field')
|
||||||
|
|
||||||
with pytest.raises(AssertionError) as exc_info:
|
field = ExampleSerializer().fields['example_field']
|
||||||
ExampleSerializer().fields
|
assert field.method_name == 'get_example_field'
|
||||||
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."
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class TestValidationErrorCode:
|
class TestValidationErrorCode:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user