diff options
| author | Tom Christie | 2013-10-10 17:34:15 +0100 | 
|---|---|---|
| committer | Tom Christie | 2013-10-10 17:34:15 +0100 | 
| commit | f18158358d99f94e2475821b1f3477dde0731bd7 (patch) | |
| tree | 84ed0ffb3cbed0b08d7281e11ec99336df2b7b75 | |
| parent | c3e370b168de03b963b6afcffce6bd52e1d4ab7c (diff) | |
| parent | 0bbc775b950d4ff7d3caef4fda719642ad792075 (diff) | |
| download | django-rest-framework-f18158358d99f94e2475821b1f3477dde0731bd7.tar.bz2 | |
Merge branch 'master' of https://github.com/tomchristie/django-rest-framework
| -rw-r--r-- | docs/api-guide/serializers.md | 15 | ||||
| -rw-r--r-- | docs/topics/credits.md | 2 | ||||
| -rw-r--r-- | docs/tutorial/1-serialization.md | 2 | ||||
| -rw-r--r-- | rest_framework/generics.py | 4 | ||||
| -rw-r--r-- | rest_framework/serializers.py | 3 | ||||
| -rw-r--r-- | rest_framework/tests/test_serializer.py | 35 | 
6 files changed, 58 insertions, 3 deletions
diff --git a/docs/api-guide/serializers.md b/docs/api-guide/serializers.md index 6b91aa76..4c3fb9d3 100644 --- a/docs/api-guide/serializers.md +++ b/docs/api-guide/serializers.md @@ -67,6 +67,21 @@ At this point we've translated the model instance into Python native datatypes.      json      # '{"email": "leila@example.com", "content": "foo bar", "created": "2012-08-22T16:20:09.822"}' +### Customizing field representation + +Sometimes when serializing objects, you may not want to represent everything exactly the way it is in your model. + +If you need to customize the serialized value of a particular field, you can do this by creating a `transform_<fieldname>` method. For example if you needed to render some markdown from a text field: + +    description = serializers.TextField() +    description_html = serializers.TextField(source='description', read_only=True) + +    def transform_description_html(self, obj, value): +        from django.contrib.markup.templatetags.markup import markdown +        return markdown(value) + +These methods are essentially the reverse of `validate_<fieldname>` (see *Validation* below.) +  ## Deserializing objects  Deserialization is similar.  First we parse a stream into Python native datatypes...  diff --git a/docs/topics/credits.md b/docs/topics/credits.md index b5dce504..586bb0f0 100644 --- a/docs/topics/credits.md +++ b/docs/topics/credits.md @@ -170,6 +170,7 @@ The following people have helped make REST framework great.  * Ben Reilly - [bwreilly]  * Tai Lee - [mrmachine]  * Markus Kaiserswerth - [mkai] +* Henry Clifford - [hcliff]  Many thanks to everyone who's contributed to the project. @@ -376,3 +377,4 @@ You can also contact [@_tomchristie][twitter] directly on twitter.  [bwreilly]: https://github.com/bwreilly  [mrmachine]: https://github.com/mrmachine  [mkai]: https://github.com/mkai +[hcliff]: https://github.com/hcliff diff --git a/docs/tutorial/1-serialization.md b/docs/tutorial/1-serialization.md index 22d29285..e1c0009c 100644 --- a/docs/tutorial/1-serialization.md +++ b/docs/tutorial/1-serialization.md @@ -225,7 +225,7 @@ For the moment we won't use any of REST framework's other features, we'll just w  We'll start off by creating a subclass of HttpResponse that we can use to render any data we return into `json`. -Edit the `snippet/views.py` file, and add the following. +Edit the `snippets/views.py` file, and add the following.      from django.http import HttpResponse      from django.views.decorators.csrf import csrf_exempt diff --git a/rest_framework/generics.py b/rest_framework/generics.py index 7d1bf794..4f134bce 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -25,13 +25,13 @@ def strict_positive_int(integer_string, cutoff=None):          ret = min(ret, cutoff)      return ret -def get_object_or_404(queryset, **filter_kwargs): +def get_object_or_404(queryset, *filter_args, **filter_kwargs):      """      Same as Django's standard shortcut, but make sure to raise 404      if the filter_kwargs don't match the required types.      """      try: -        return _get_object_or_404(queryset, **filter_kwargs) +        return _get_object_or_404(queryset, *filter_args, **filter_kwargs)      except (TypeError, ValueError):          raise Http404 diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 8e945688..33db82ee 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -315,6 +315,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] = self.augment_field(field, field_name, key, value) diff --git a/rest_framework/tests/test_serializer.py b/rest_framework/tests/test_serializer.py index d4e5a93f..29c08fbc 100644 --- a/rest_framework/tests/test_serializer.py +++ b/rest_framework/tests/test_serializer.py @@ -1658,3 +1658,38 @@ class SerializerSupportsManyRelationships(TestCase):          serializer = SimpleSlugSourceModelSerializer(data={'text': 'foo', 'targets': [1, 2]})          self.assertTrue(serializer.is_valid())          self.assertEqual(serializer.data, {'text': 'foo', 'targets': [1, 2]}) + + +class TransformMethodsSerializer(serializers.Serializer): +    a = serializers.CharField() +    b_renamed = serializers.CharField(source='b') + +    def transform_a(self, obj, value): +        return value.lower() + +    def transform_b_renamed(self, obj, value): +        if value is not None: +            return 'and ' + value + + +class TestSerializerTransformMethods(TestCase): +    def setUp(self): +        self.s = TransformMethodsSerializer() + +    def test_transform_methods(self): +        self.assertEqual( +            self.s.to_native({'a': 'GREEN EGGS', 'b': 'HAM'}), +            { +                'a': 'green eggs', +                'b_renamed': 'and HAM', +            } +        ) + +    def test_missing_fields(self): +        self.assertEqual( +            self.s.to_native({'a': 'GREEN EGGS'}), +            { +                'a': 'green eggs', +                'b_renamed': None, +            } +        )  | 
