From cb43898d49ac60bf84a2eb83b331072e7651af5e Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Mon, 13 Apr 2009 12:35:49 +0000 Subject: 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 --- django/core/serializers/python.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'django/core/serializers/python.py') 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) -- cgit v1.3