aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/fields.py
diff options
context:
space:
mode:
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 01cf5ae3..9dce0143 100644
--- a/rest_framework/fields.py
+++ b/rest_framework/fields.py
@@ -1030,3 +1030,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)