aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--rest_framework/fields.py21
-rw-r--r--rest_framework/serializers.py3
-rw-r--r--rest_framework/utils/modelinfo.py3
-rw-r--r--tests/test_serializer_empty.py2
4 files changed, 14 insertions, 15 deletions
diff --git a/rest_framework/fields.py b/rest_framework/fields.py
index a56ea96b..5f198767 100644
--- a/rest_framework/fields.py
+++ b/rest_framework/fields.py
@@ -768,16 +768,13 @@ class ReadOnlyField(Field):
kwargs['read_only'] = True
super(ReadOnlyField, self).__init__(**kwargs)
- def to_native(self, data):
- raise NotImplemented('.to_native() not supported.')
-
def to_primative(self, value):
if is_simple_callable(value):
return value()
return value
-class MethodField(Field):
+class SerializerMethodField(Field):
"""
A read-only field that get its representation from calling a method on the
parent serializer class. The method called will be of the form
@@ -787,22 +784,22 @@ class MethodField(Field):
For example:
class ExampleSerializer(self):
- extra_info = MethodField()
+ extra_info = SerializerMethodField()
def get_extra_info(self, obj):
return ... # Calculate some data to return.
"""
- def __init__(self, **kwargs):
+ def __init__(self, method_attr=None, **kwargs):
+ self.method_attr = method_attr
kwargs['source'] = '*'
kwargs['read_only'] = True
- super(MethodField, self).__init__(**kwargs)
-
- def to_native(self, data):
- raise NotImplemented('.to_native() not supported.')
+ super(SerializerMethodField, self).__init__(**kwargs)
def to_primative(self, value):
- attr = 'get_{field_name}'.format(field_name=self.field_name)
- method = getattr(self.parent, attr)
+ method_attr = self.method_attr
+ if method_attr is None:
+ method_attr = 'get_{field_name}'.format(field_name=self.field_name)
+ method = getattr(self.parent, method_attr)
return method(value)
diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py
index 4322f213..388fe29f 100644
--- a/rest_framework/serializers.py
+++ b/rest_framework/serializers.py
@@ -598,7 +598,8 @@ class ModelSerializer(Serializer):
if isinstance(model_field, models.BooleanField):
# models.BooleanField has `blank=True`, but *is* actually
# required *unless* a default is provided.
- # Also note that <1.6 `default=False`, >=1.6 `default=None`.
+ # Also note that Django<1.6 uses `default=False` for
+ # models.BooleanField, but Django>=1.6 uses `default=None`.
kwargs.pop('required', None)
if validator_kwarg:
diff --git a/rest_framework/utils/modelinfo.py b/rest_framework/utils/modelinfo.py
index a7a0346c..960fa4d0 100644
--- a/rest_framework/utils/modelinfo.py
+++ b/rest_framework/utils/modelinfo.py
@@ -1,6 +1,7 @@
"""
Helper functions for returning the field information that is associated
-with a model class.
+with a model class. This includes returning all the forward and reverse
+relationships and their associated metadata.
"""
from collections import namedtuple
from django.db import models
diff --git a/tests/test_serializer_empty.py b/tests/test_serializer_empty.py
index 4e4a7b42..805ac7d4 100644
--- a/tests/test_serializer_empty.py
+++ b/tests/test_serializer_empty.py
@@ -6,7 +6,7 @@
# def test_empty_serializer(self):
# class FooBarSerializer(serializers.Serializer):
# foo = serializers.IntegerField()
-# bar = serializers.MethodField()
+# bar = serializers.SerializerMethodField()
# def get_bar(self, obj):
# return 'bar'