1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

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.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10554 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee
2009-04-13 12:35:49 +00:00
parent fd3ee7d786
commit cb43898d49
4 changed files with 95 additions and 9 deletions

View File

@@ -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)

View File

@@ -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")