mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-06-18 20:43:29 +03:00
Decimals are a protected_type - let's leave them up to the renderer to deal with
This commit is contained in:
parent
f02a4e1890
commit
82de0cf50a
|
@ -108,6 +108,7 @@ class XMLRenderer(BaseRenderer):
|
||||||
"""
|
"""
|
||||||
Renderer which serializes to XML.
|
Renderer which serializes to XML.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
media_type = 'application/xml'
|
media_type = 'application/xml'
|
||||||
|
|
||||||
def render(self, obj=None, media_type=None):
|
def render(self, obj=None, media_type=None):
|
||||||
|
@ -251,6 +252,7 @@ class DocumentingTemplateRenderer(BaseRenderer):
|
||||||
The context used in the template contains all the information
|
The context used in the template contains all the information
|
||||||
needed to self-document the response to this request.
|
needed to self-document the response to this request.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
content = self._get_content(self.view, self.view.request, obj, media_type)
|
content = self._get_content(self.view, self.view.request, obj, media_type)
|
||||||
|
|
||||||
put_form_instance = self._get_form_instance(self.view, 'put')
|
put_form_instance = self._get_form_instance(self.view, 'put')
|
||||||
|
|
|
@ -4,7 +4,7 @@ Customizable serialization.
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db.models.query import QuerySet
|
from django.db.models.query import QuerySet
|
||||||
from django.db.models.fields.related import RelatedField
|
from django.db.models.fields.related import RelatedField
|
||||||
from django.utils.encoding import smart_unicode
|
from django.utils.encoding import smart_unicode, is_protected_type
|
||||||
|
|
||||||
import decimal
|
import decimal
|
||||||
import inspect
|
import inspect
|
||||||
|
@ -273,13 +273,6 @@ class Serializer(object):
|
||||||
return self.serialize_iter(obj.all())
|
return self.serialize_iter(obj.all())
|
||||||
|
|
||||||
|
|
||||||
def serialize_decimal(self, obj):
|
|
||||||
"""
|
|
||||||
Convert a Decimal instance into a serializable representation.
|
|
||||||
"""
|
|
||||||
return str(obj)
|
|
||||||
|
|
||||||
|
|
||||||
def serialize_fallback(self, obj):
|
def serialize_fallback(self, obj):
|
||||||
"""
|
"""
|
||||||
Convert any unhandled object into a serializable representation.
|
Convert any unhandled object into a serializable representation.
|
||||||
|
@ -301,9 +294,6 @@ class Serializer(object):
|
||||||
elif isinstance(obj, models.Manager):
|
elif isinstance(obj, models.Manager):
|
||||||
# Manager objects
|
# Manager objects
|
||||||
return self.serialize_manager(obj)
|
return self.serialize_manager(obj)
|
||||||
elif isinstance(obj, decimal.Decimal):
|
|
||||||
# Decimals (force to string representation)
|
|
||||||
return self.serialize_decimal(obj)
|
|
||||||
elif inspect.isfunction(obj) and not inspect.getargspec(obj)[0]:
|
elif inspect.isfunction(obj) and not inspect.getargspec(obj)[0]:
|
||||||
# function with no args
|
# function with no args
|
||||||
return self.serialize_func(obj)
|
return self.serialize_func(obj)
|
||||||
|
@ -311,5 +301,10 @@ class Serializer(object):
|
||||||
# bound method
|
# bound method
|
||||||
return self.serialize_func(obj)
|
return self.serialize_func(obj)
|
||||||
|
|
||||||
# fall back to smart unicode
|
# Protected types are passed through as is.
|
||||||
|
# (i.e. Primitives like None, numbers, dates, and Decimals.)
|
||||||
|
if is_protected_type(obj):
|
||||||
|
return obj
|
||||||
|
|
||||||
|
# All other values are converted to string.
|
||||||
return self.serialize_fallback(obj)
|
return self.serialize_fallback(obj)
|
||||||
|
|
|
@ -18,7 +18,7 @@ class TestObjectToData(TestCase):
|
||||||
|
|
||||||
def test_decimal(self):
|
def test_decimal(self):
|
||||||
"""Decimals need to be converted to a string representation."""
|
"""Decimals need to be converted to a string representation."""
|
||||||
self.assertEquals(self.serialize(decimal.Decimal('1.5')), '1.5')
|
self.assertEquals(self.serialize(decimal.Decimal('1.5')), decimal.Decimal('1.5'))
|
||||||
|
|
||||||
def test_function(self):
|
def test_function(self):
|
||||||
"""Functions with no arguments should be called."""
|
"""Functions with no arguments should be called."""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user