diff options
| author | Russell Keith-Magee <russell@keith-magee.com> | 2009-04-13 12:35:49 +0000 |
|---|---|---|
| committer | Russell Keith-Magee <russell@keith-magee.com> | 2009-04-13 12:35:49 +0000 |
| commit | cb43898d49ac60bf84a2eb83b331072e7651af5e (patch) | |
| tree | 1dfed754b16d60b3791feaca9d420010864b72ba /django/utils/encoding.py | |
| parent | fd3ee7d7867dd13a336b90873ccb8df6302f2c05 (diff) | |
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
Diffstat (limited to 'django/utils/encoding.py')
| -rw-r--r-- | django/utils/encoding.py | 15 |
1 files changed, 14 insertions, 1 deletions
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,): |
