From 94cd1369437f84adefb46462439b46dc5208ab1d Mon Sep 17 00:00:00 2001 From: Craig de Stigter Date: Thu, 29 Aug 2013 17:35:15 +1200 Subject: [PATCH] add transform_ methods to serializers, which basically do the opposite of validate_ on a per-field basis. --- rest_framework/serializers.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 31cfa3447..8ba1b1959 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -304,6 +304,9 @@ class BaseSerializer(WritableField): field.initialize(parent=self, field_name=field_name) key = self.get_field_key(field_name) value = field.field_to_native(obj, field_name) + method = getattr(self, 'transform_%s' % field_name, None) + if callable(method): + value = method(obj, value) ret[key] = value ret.fields[key] = field return ret