Skip to content

Commit 91ea138

Browse files
rpkilbytomchristie
authored andcommitted
Allow redundant SerializerMethodField method names (#6767)
1 parent 42fd179 commit 91ea138

File tree

2 files changed

+4
-14
lines changed

2 files changed

+4
-14
lines changed

rest_framework/fields.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,12 +1835,6 @@ def bind(self, field_name, parent):
18351835
# 'method_name' argument has been used. For example:
18361836
# my_field = serializer.SerializerMethodField(method_name='get_my_field')
18371837
default_method_name = 'get_{field_name}'.format(field_name=field_name)
1838-
assert self.method_name != default_method_name, (
1839-
"It is redundant to specify `%s` on SerializerMethodField '%s' in "
1840-
"serializer '%s', because it is the same as the default method name. "
1841-
"Remove the `method_name` argument." %
1842-
(self.method_name, field_name, parent.__class__.__name__)
1843-
)
18441838

18451839
# The method name should default to `get_{field_name}`.
18461840
if self.method_name is None:

tests/test_fields.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2212,17 +2212,13 @@ def get_example_field(self, obj):
22122212
}
22132213

22142214
def test_redundant_method_name(self):
2215+
# Prior to v3.10, redundant method names were not allowed.
2216+
# This restriction has since been removed.
22152217
class ExampleSerializer(serializers.Serializer):
22162218
example_field = serializers.SerializerMethodField('get_example_field')
22172219

2218-
with pytest.raises(AssertionError) as exc_info:
2219-
ExampleSerializer().fields
2220-
assert str(exc_info.value) == (
2221-
"It is redundant to specify `get_example_field` on "
2222-
"SerializerMethodField 'example_field' in serializer "
2223-
"'ExampleSerializer', because it is the same as the default "
2224-
"method name. Remove the `method_name` argument."
2225-
)
2220+
field = ExampleSerializer().fields['example_field']
2221+
assert field.method_name == 'get_example_field'
22262222

22272223

22282224
class TestValidationErrorCode:

0 commit comments

Comments
 (0)