diff options
| author | Tom Christie | 2012-11-20 15:30:30 -0800 |
|---|---|---|
| committer | Tom Christie | 2012-11-20 15:30:30 -0800 |
| commit | 8e8b23b6a9aece32ae5c028a72969777f1ddc7ae (patch) | |
| tree | 786a0048b9ab07cee7dbabab6574cec1ced4781a /rest_framework/fields.py | |
| parent | 3227a357cec2475b8295a67e9fd66f644ea5b0cd (diff) | |
| parent | 5f4c385a86b877217c1e1bc2eaff58206eabb747 (diff) | |
| download | django-rest-framework-8e8b23b6a9aece32ae5c028a72969777f1ddc7ae.tar.bz2 | |
Merge pull request #430 from j4mie/serializer-method-field
Serializer method field
Diffstat (limited to 'rest_framework/fields.py')
| -rw-r--r-- | rest_framework/fields.py | 14 |
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) |
