aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/fields.py
diff options
context:
space:
mode:
authorJamie Matthews2012-11-19 17:22:17 +0000
committerJamie Matthews2012-11-19 17:22:17 +0000
commitde5b071d677074ab3b6b33a843c4b05ba2052a6b (patch)
treeb19923214d4befe95603c827d828cb2c4f5c68b8 /rest_framework/fields.py
parentd1472740bc5f08871343d1a63e409e34d05504cb (diff)
downloaddjango-rest-framework-de5b071d677074ab3b6b33a843c4b05ba2052a6b.tar.bz2
Add SerializerMethodField
Diffstat (limited to 'rest_framework/fields.py')
-rw-r--r--rest_framework/fields.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/rest_framework/fields.py b/rest_framework/fields.py
index c68c39b5..d1e9c45d 100644
--- a/rest_framework/fields.py
+++ b/rest_framework/fields.py
@@ -1019,3 +1019,17 @@ class ImageField(FileField):
if hasattr(f, 'seek') and callable(f.seek):
f.seek(0)
return f
+
+
+class SerializerMethodField(Field):
+ """
+ A field that gets its value by calling a method on the serializer it's attached to.
+ """
+
+ def __init__(self, method_name):
+ self.method_name = method_name
+ super(SerializerMethodField, self).__init__()
+
+ def field_to_native(self, obj, field_name):
+ value = getattr(self.parent, self.method_name)(obj)
+ return self.to_native(value)