Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #5553 -- Fixed a serialization problem with datetime and time o…

…bjects. Thanks to pigletto for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6406 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit f2101abacff1069303d2afa204106e049c633654 1 parent 694323e
@freakboy3742 freakboy3742 authored
Showing with 14 additions and 3 deletions.
  1. +2 −1  django/utils/encoding.py
  2. +12 −2 tests/modeltests/serializers/models.py
View
3  django/utils/encoding.py
@@ -1,5 +1,6 @@
import types
import urllib
+import datetime
from django.utils.functional import Promise
class StrAndUnicode(object):
@@ -30,7 +31,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)):
+ if strings_only and isinstance(s, (types.NoneType, int, long, datetime.datetime, datetime.time, float)):
return s
if not isinstance(s, basestring,):
if hasattr(s, '__unicode__'):
View
14 tests/modeltests/serializers/models.py
@@ -63,6 +63,9 @@ class Meta:
def __unicode__(self):
return self.title
+
+class Score(models.Model):
+ score = models.FloatField()
__test__ = {'API_TESTS':"""
# Create some data:
@@ -83,7 +86,7 @@ def __unicode__(self):
>>> a2 = Article(
... author = joe,
... headline = "Time to reform copyright",
-... pub_date = datetime(2006, 6, 16, 13, 00))
+... pub_date = datetime(2006, 6, 16, 13, 00, 11, 345))
>>> a1.save(); a2.save()
>>> a1.categories = [sports, op_ed]
>>> a2.categories = [music, op_ed]
@@ -181,7 +184,7 @@ def __unicode__(self):
# Serializer output can be restricted to a subset of fields
>>> print serializers.serialize("json", Article.objects.all(), fields=('headline','pub_date'))
-[{"pk": 1, "model": "serializers.article", "fields": {"headline": "Just kidding; I love TV poker", "pub_date": "2006-06-16 11:00:00"}}, {"pk": 2, "model": "serializers.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 3, "model": "serializers.article", "fields": {"headline": "Forward references pose no problem", "pub_date": "2006-06-16 15:00:00"}}]
+[{"pk": 1, "model": "serializers.article", "fields": {"headline": "Just kidding; I love TV poker", "pub_date": "2006-06-16 11:00:00"}}, {"pk": 2, "model": "serializers.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:11"}}, {"pk": 3, "model": "serializers.article", "fields": {"headline": "Forward references pose no problem", "pub_date": "2006-06-16 15:00:00"}}]
# Every string is serialized as a unicode object, also primary key
# which is 'varchar'
@@ -207,4 +210,11 @@ def __unicode__(self):
>>> print list(serializers.deserialize('json', serializers.serialize('json', [mv2])))[0].object.id
None
+# Serialization and deserialization of floats:
+>>> sc = Score(score=3.4)
+>>> print serializers.serialize("json", [sc])
+[{"pk": null, "model": "serializers.score", "fields": {"score": 3.4}}]
+>>> print list(serializers.deserialize('json', serializers.serialize('json', [sc])))[0].object.score
+3.4
+
"""}
Please sign in to comment.
Something went wrong with that request. Please try again.