summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorRussell Keith-Magee <russell@keith-magee.com>2009-04-13 12:40:43 +0000
committerRussell Keith-Magee <russell@keith-magee.com>2009-04-13 12:40:43 +0000
commit960d3172f630b80c909ffb517356fdb3161d2d3a (patch)
tree1a81ccdf272e76944120d693cba0a265a0ec0ddf /django
parent6be2d903f328421e11a0da3c3321d15ceaeb1656 (diff)
[1.0.X] Fixed #9522 -- Modified handling of values in base serializer so that field subclasses can define their own value_to_string() method for serialization. Thanks to Alex Koshelev for the report and initial patch.
Merge of r10554 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10555 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
-rw-r--r--django/core/serializers/python.py15
-rw-r--r--django/core/serializers/xml_serializer.py6
-rw-r--r--django/utils/encoding.py15
3 files changed, 27 insertions, 9 deletions
diff --git a/django/core/serializers/python.py b/django/core/serializers/python.py
index 0eeb485797..b672e4efc3 100644
--- a/django/core/serializers/python.py
+++ b/django/core/serializers/python.py
@@ -7,15 +7,15 @@ other serializers.
from django.conf import settings
from django.core.serializers import base
from django.db import models
-from django.utils.encoding import smart_unicode
+from django.utils.encoding import smart_unicode, is_protected_type
class Serializer(base.Serializer):
"""
Serializes a QuerySet to basic Python objects.
"""
-
+
internal_use_only = True
-
+
def start_serialization(self):
self._current = None
self.objects = []
@@ -35,7 +35,14 @@ class Serializer(base.Serializer):
self._current = None
def handle_field(self, obj, field):
- self._current[field.name] = smart_unicode(getattr(obj, field.name), strings_only=True)
+ value = field._get_val_from_obj(obj)
+ # Protected types (i.e., primitives like None, numbers, dates,
+ # and Decimals) are passed through as is. All other values are
+ # converted to string first.
+ if is_protected_type(value):
+ self._current[field.name] = value
+ else:
+ self._current[field.name] = field.value_to_string(obj)
def handle_fk_field(self, obj, field):
related = getattr(obj, field.name)
diff --git a/django/core/serializers/xml_serializer.py b/django/core/serializers/xml_serializer.py
index 04498db00c..2d74fe28f3 100644
--- a/django/core/serializers/xml_serializer.py
+++ b/django/core/serializers/xml_serializer.py
@@ -65,11 +65,9 @@ class Serializer(base.Serializer):
"type" : field.get_internal_type()
})
- # Get a "string version" of the object's data (this is handled by the
- # serializer base class).
+ # Get a "string version" of the object's data.
if getattr(obj, field.name) is not None:
- value = self.get_string_value(obj, field)
- self.xml.characters(smart_unicode(value))
+ self.xml.characters(field.value_to_string(obj))
else:
self.xml.addQuickElement("None")
diff --git a/django/utils/encoding.py b/django/utils/encoding.py
index 7e87b05c79..9388d67f4c 100644
--- a/django/utils/encoding.py
+++ b/django/utils/encoding.py
@@ -41,6 +41,19 @@ def smart_unicode(s, encoding='utf-8', strings_only=False, errors='strict'):
return s
return force_unicode(s, encoding, strings_only, errors)
+def is_protected_type(obj):
+ """Determine if the object instance is of a protected type.
+
+ Objects of protected types are preserved as-is when passed to
+ force_unicode(strings_only=True).
+ """
+ return isinstance(obj, (
+ types.NoneType,
+ int, long,
+ datetime.datetime, datetime.date, datetime.time,
+ float, Decimal)
+ )
+
def force_unicode(s, encoding='utf-8', strings_only=False, errors='strict'):
"""
Similar to smart_unicode, except that lazy instances are resolved to
@@ -48,7 +61,7 @@ def force_unicode(s, encoding='utf-8', strings_only=False, errors='strict'):
If strings_only is True, don't convert (some) non-string-like objects.
"""
- if strings_only and isinstance(s, (types.NoneType, int, long, datetime.datetime, datetime.date, datetime.time, float, Decimal)):
+ if strings_only and is_protected_type(s):
return s
try:
if not isinstance(s, basestring,):