mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-05 13:00:12 +03:00
Remove assertions for redundant SerializerMethodField source args.
This commit is contained in:
parent
4ce4132e08
commit
8eff850b09
|
@ -699,18 +699,6 @@ The argument to `SerializerMethodField` is now optional, and defaults to `get_<f
|
|||
def get_billing_details(self, account):
|
||||
return calculate_billing(account)
|
||||
|
||||
In order to ensure a consistent code style an assertion error will be raised if you include a redundant method name argument that matches the default method name. For example, the following code *will raise an error*:
|
||||
|
||||
billing_details = serializers.SerializerMethodField('get_billing_details')
|
||||
|
||||
#### Enforcing consistent `source` usage.
|
||||
|
||||
I've see several codebases that unnecessarily include the `source` argument, setting it to the same value as the field name. This usage is redundant and confusing, making it less obvious that `source` is usually not required.
|
||||
|
||||
The following usage will *now raise an error*:
|
||||
|
||||
email = serializers.EmailField(source='email')
|
||||
|
||||
#### The `UniqueValidator` and `UniqueTogetherValidator` classes.
|
||||
|
||||
REST framework now provides new validators that allow you to ensure field uniqueness, while still using a completely explicit `Serializer` class instead of using `ModelSerializer`.
|
||||
|
|
|
@ -210,16 +210,6 @@ class Field(object):
|
|||
Called when a field is added to the parent serializer instance.
|
||||
"""
|
||||
|
||||
# In order to enforce a consistent style, we error if a redundant
|
||||
# 'source' argument has been used. For example:
|
||||
# my_field = serializer.CharField(source='my_field')
|
||||
assert self.source != field_name, (
|
||||
"It is redundant to specify `source='%s'` on field '%s' in "
|
||||
"serializer '%s', because it is the same as the field name. "
|
||||
"Remove the `source` keyword argument." %
|
||||
(field_name, self.__class__.__name__, parent.__class__.__name__)
|
||||
)
|
||||
|
||||
self.field_name = field_name
|
||||
self.parent = parent
|
||||
|
||||
|
@ -1219,17 +1209,7 @@ class SerializerMethodField(Field):
|
|||
super(SerializerMethodField, self).__init__(**kwargs)
|
||||
|
||||
def bind(self, field_name, parent):
|
||||
# In order to enforce a consistent style, we error if a redundant
|
||||
# 'method_name' argument has been used. For example:
|
||||
# my_field = serializer.CharField(source='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:
|
||||
self.method_name = default_method_name
|
||||
|
|
|
@ -81,17 +81,6 @@ class TestSource:
|
|||
assert serializer.is_valid()
|
||||
assert serializer.validated_data == {'other': 'abc'}
|
||||
|
||||
def test_redundant_source(self):
|
||||
class ExampleSerializer(serializers.Serializer):
|
||||
example_field = serializers.CharField(source='example_field')
|
||||
with pytest.raises(AssertionError) as exc_info:
|
||||
ExampleSerializer().fields
|
||||
assert str(exc_info.value) == (
|
||||
"It is redundant to specify `source='example_field'` on field "
|
||||
"'CharField' in serializer 'ExampleSerializer', because it is the "
|
||||
"same as the field name. Remove the `source` keyword argument."
|
||||
)
|
||||
|
||||
|
||||
class TestReadOnly:
|
||||
def setup(self):
|
||||
|
@ -1078,16 +1067,3 @@ class TestSerializerMethodField:
|
|||
assert serializer.data == {
|
||||
'example_field': 'ran get_example_field(123)'
|
||||
}
|
||||
|
||||
def test_redundant_method_name(self):
|
||||
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."
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue
Block a user