Repository: encode/django-rest-framework

Issue:
YAMLRenderer - RepresenterError : cannot represent an object: ReturnDict
Tested with django-rest-framework 3.0 dd712a1c2620b5dc9ad8eef5ff78ef232feb12e8 under python 3.4 and with a recent django (1.8+)

```
Environment:

Request Method: GET
Request URL: http://192.168.56.101/fr/api/v1/media/5604d858-f592-4338-aaa3-73293364c8db?format=yaml

Django Version: 1.8.dev20141120151358
Python Version: 3.4.0
Installed Applications:
['django_admin_bootstrapped',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.humanize',
 'django.contrib.messages',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.staticfiles',
  ...
 'rest_framework',
 'debug_toolbar',
 'template_timings_panel']
Installed Middleware:
['debug_toolbar.middleware.DebugToolbarMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'project.middleware.locale.LocaleMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Traceback:
File "/var/project/prod/venv/src/django/django/core/handlers/base.py" in get_response
  157.                 response = response.render()
File "/var/project/prod/venv/src/django/django/template/response.py" in render
  104.             self.content = self.rendered_content
File "/var/project/prod/venv/src/django-rest-framework/rest_framework/response.py" in rendered_content
  59.         ret = renderer.render(self.data, media_type, context)
File "/var/project/prod/venv/src/django-rest-framework/rest_framework/renderers.py" in render
  218.         return yaml.dump(data, stream=None, encoding=self.charset, Dumper=self.encoder, allow_unicode=not self.ensure_ascii)
File "/usr/local/lib/python3.4/dist-packages/yaml/__init__.py" in dump
  200.     return dump_all([data], stream, Dumper=Dumper, **kwds)
File "/usr/local/lib/python3.4/dist-packages/yaml/__init__.py" in dump_all
  188.             dumper.represent(data)
File "/usr/local/lib/python3.4/dist-packages/yaml/representer.py" in represent
  26.         node = self.represent_data(data)
File "/usr/local/lib/python3.4/dist-packages/yaml/representer.py" in represent_data
  57.                     node = self.yaml_representers[None](self, data)
File "/usr/local/lib/python3.4/dist-packages/yaml/representer.py" in represent_undefined
  227.         raise RepresenterError("cannot represent an object: %s" % data)

Exception Type: RepresenterError at /fr/api/v1/media/5604d858-f592-4338-aaa3-73293364c8db
Exception Value: cannot represent an object: ReturnDict([('api_url', 'http://192.168.56.101/fr/api/v1/media/5604d858-f592-4338-aaa3-73293364c8db'), ('uuid', '5604d858-f592-4338-aaa3-73293364c8db'), ('title', 'Divergent 1080p (transcoded)'), ('file', 'http://192.168.56.101/protected/media/5604d858-f592-4338-aaa3-73293364c8db/Divergent%201080p%2000%3A02%3A40-00%3A03%3A40%20transcoded.mkv'), ('url', None), ('md5_checksum', None), ('metadatas', None), ('keyword_set', []), ('state', 'BROKEN'), ('parent', 'http://192.168.56.101/fr/api/v1/media/5ff1ba6f-c7c5-45ff-ae28-29b3621b5d98'), ('created_by', 2), ('created_at', '2014-12-11T08:30:45.052558Z'), ('updated_at', '2014-12-11T08:30:45.254961Z'), ('infos', None), ('is_upload', False), ('meta', OrderedDict([('duration', '0:00:00'), ('framerate', 0.0), ('interlaced', None), ('size', 0), ('width', 0), ('height', 0), ('content_width', 0), ('content_height', 0), ('content_x', 0), ('content_y', 0)]))])
```



Discussion:
I think we need some more information here. Can you share some code? Was this also happening from the latest released version in PyPI?

Verified. Run the quickstart and add `rest_framework.renderers.YAMLRenderer` to your `REST_FRAMEWORK` settings dict.


Resolving patch:
diff --git a/rest_framework/utils/encoders.py b/rest_framework/utils/encoders.py
--- a/rest_framework/utils/encoders.py
+++ b/rest_framework/utils/encoders.py
@@ -7,6 +7,7 @@
 from django.utils.encoding import force_text
 from django.utils.functional import Promise
 from rest_framework.compat import OrderedDict
+from rest_framework.utils.serializer_helpers import ReturnDict, ReturnList
 import datetime
 import decimal
 import types
@@ -107,14 +108,14 @@ def represent_mapping(self, tag, mapping, flow_style=None):
         OrderedDict,
         yaml.representer.SafeRepresenter.represent_dict
     )
-    # SafeDumper.add_representer(
-    #     DictWithMetadata,
-    #     yaml.representer.SafeRepresenter.represent_dict
-    # )
-    # SafeDumper.add_representer(
-    #     OrderedDictWithMetadata,
-    #     yaml.representer.SafeRepresenter.represent_dict
-    # )
+    SafeDumper.add_representer(
+        ReturnDict,
+        yaml.representer.SafeRepresenter.represent_dict
+    )
+    SafeDumper.add_representer(
+        ReturnList,
+        yaml.representer.SafeRepresenter.represent_list
+    )
     SafeDumper.add_representer(
         types.GeneratorType,
         yaml.representer.SafeRepresenter.represent_list


Test patch: