aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTom Christie2013-10-17 01:38:27 -0700
committerTom Christie2013-10-17 01:38:27 -0700
commitb3e01452947f2d69f2582ecb8d8cba9ce70aadb5 (patch)
tree4f441988d43e884fa77dc73d8465bb2c8b4ae949
parentbdf8b532180f465c035fca5da121ab1de8a057e1 (diff)
parentb730aec0f46e2b849b3c597bcf1a1bcdc158e415 (diff)
downloaddjango-rest-framework-b3e01452947f2d69f2582ecb8d8cba9ce70aadb5.tar.bz2
Merge pull request #1181 from badale/master
Fixed #1179: RepresenterError with DecimalField and YAMLRenderer
-rw-r--r--rest_framework/tests/test_renderers.py13
-rw-r--r--rest_framework/utils/encoders.py3
2 files changed, 15 insertions, 1 deletions
diff --git a/rest_framework/tests/test_renderers.py b/rest_framework/tests/test_renderers.py
index df6f4aa6..76299a89 100644
--- a/rest_framework/tests/test_renderers.py
+++ b/rest_framework/tests/test_renderers.py
@@ -328,7 +328,7 @@ if yaml:
class YAMLRendererTests(TestCase):
"""
- Tests specific to the JSON Renderer
+ Tests specific to the YAML Renderer
"""
def test_render(self):
@@ -354,6 +354,17 @@ if yaml:
data = parser.parse(StringIO(content))
self.assertEqual(obj, data)
+ def test_render_decimal(self):
+ """
+ Test YAML decimal rendering.
+ """
+ renderer = YAMLRenderer()
+ content = renderer.render({'field': Decimal('111.2')}, 'application/yaml')
+ self.assertYAMLContains(content, "field: '111.2'")
+
+ def assertYAMLContains(self, content, string):
+ self.assertTrue(string in content, '%r not in %r' % (string, content))
+
class XMLRendererTestCase(TestCase):
"""
diff --git a/rest_framework/utils/encoders.py b/rest_framework/utils/encoders.py
index 7efd5417..35ad206b 100644
--- a/rest_framework/utils/encoders.py
+++ b/rest_framework/utils/encoders.py
@@ -89,6 +89,9 @@ else:
node.flow_style = best_style
return node
+ SafeDumper.add_representer(decimal.Decimal,
+ SafeDumper.represent_decimal)
+
SafeDumper.add_representer(SortedDict,
yaml.representer.SafeRepresenter.represent_dict)
SafeDumper.add_representer(DictWithMetadata,