diff options
| author | Tom Christie | 2014-09-25 12:09:12 +0100 |
|---|---|---|
| committer | Tom Christie | 2014-09-25 12:09:12 +0100 |
| commit | b47ca158b9ba9733baad080e648d24b0465ec697 (patch) | |
| tree | 68721eeca909bf53dedea6d86f2894ec0eafad0a /rest_framework/fields.py | |
| parent | 64632da3718f501cb8174243385d38b547c2fefd (diff) | |
| download | django-rest-framework-b47ca158b9ba9733baad080e648d24b0465ec697.tar.bz2 | |
Check for redundant on SerializerMethodField
Diffstat (limited to 'rest_framework/fields.py')
| -rw-r--r-- | rest_framework/fields.py | 29 |
1 files changed, 22 insertions, 7 deletions
diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 446732c3..328e93ef 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -178,7 +178,7 @@ class Field(object): # 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._kwargs.get('source') != field_name, ( + 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." % @@ -883,17 +883,32 @@ class SerializerMethodField(Field): def get_extra_info(self, obj): return ... # Calculate some data to return. """ - def __init__(self, method_attr=None, **kwargs): - self.method_attr = method_attr + def __init__(self, method_name=None, **kwargs): + self.method_name = method_name kwargs['source'] = '*' kwargs['read_only'] = True 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 + + super(SerializerMethodField, self).bind(field_name, parent) + def to_representation(self, value): - method_attr = self.method_attr - if method_attr is None: - method_attr = 'get_{field_name}'.format(field_name=self.field_name) - method = getattr(self.parent, method_attr) + method = getattr(self.parent, self.method_name) return method(value) |
