From fb1546ee50953faae8af505a0c90da00ac08ad92 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 24 Sep 2014 20:53:37 +0100 Subject: Enforce field_name != source --- rest_framework/fields.py | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'rest_framework/fields.py') diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 1f7d964a..9280ea3a 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -160,6 +160,17 @@ class Field(object): """ Setup the context for the field 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._kwargs.get('source') != field_name, ( + "It is redundant to specify `source='%s'` on field '%s' in " + "serializer '%s', as it is the same the field name. " + "Remove the `source` keyword argument." % + (field_name, self.__class__.__name__, parent.__class__.__name__) + ) + self.field_name = field_name self.parent = parent self.root = root -- cgit v1.2.3