Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Russell Keith-Magee authored September 22, 2007
3  django/utils/encoding.py
... ...
@@ -1,5 +1,6 @@
1 1
 import types
2 2
 import urllib
  3
+import datetime
3 4
 from django.utils.functional import Promise
4 5
 
5 6
 class StrAndUnicode(object):
@@ -30,7 +31,7 @@ def force_unicode(s, encoding='utf-8', strings_only=False, errors='strict'):
30 31
 
31 32
     If strings_only is True, don't convert (some) non-string-like objects.
32 33
     """
33  
-    if strings_only and isinstance(s, (types.NoneType, int, long)):
  34
+    if strings_only and isinstance(s, (types.NoneType, int, long, datetime.datetime, datetime.time, float)):
34 35
         return s
35 36
     if not isinstance(s, basestring,):
36 37
         if hasattr(s, '__unicode__'):
14  tests/modeltests/serializers/models.py
@@ -63,6 +63,9 @@ class Meta:
63 63
 
64 64
     def __unicode__(self):
65 65
         return self.title
  66
+        
  67
+class Score(models.Model):
  68
+    score = models.FloatField()
66 69
 
67 70
 __test__ = {'API_TESTS':"""
68 71
 # Create some data:
@@ -83,7 +86,7 @@ def __unicode__(self):
83 86
 >>> a2 = Article(
84 87
 ...     author = joe,
85 88
 ...     headline = "Time to reform copyright",
86  
-...     pub_date = datetime(2006, 6, 16, 13, 00))
  89
+...     pub_date = datetime(2006, 6, 16, 13, 00, 11, 345))
87 90
 >>> a1.save(); a2.save()
88 91
 >>> a1.categories = [sports, op_ed]
89 92
 >>> a2.categories = [music, op_ed]
@@ -181,7 +184,7 @@ def __unicode__(self):
181 184
 
182 185
 # Serializer output can be restricted to a subset of fields
183 186
 >>> print serializers.serialize("json", Article.objects.all(), fields=('headline','pub_date'))
184  
-[{"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"}}]
  187
+[{"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"}}]
185 188
 
186 189
 # Every string is serialized as a unicode object, also primary key 
187 190
 # which is 'varchar'
@@ -207,4 +210,11 @@ def __unicode__(self):
207 210
 >>> print list(serializers.deserialize('json', serializers.serialize('json', [mv2])))[0].object.id
208 211
 None
209 212
 
  213
+# Serialization and deserialization of floats:
  214
+>>> sc = Score(score=3.4)
  215
+>>> print serializers.serialize("json", [sc])
  216
+[{"pk": null, "model": "serializers.score", "fields": {"score": 3.4}}]
  217
+>>> print list(serializers.deserialize('json', serializers.serialize('json', [sc])))[0].object.score
  218
+3.4
  219
+
210 220
 """}

0 notes on commit f2101ab

Please sign in to comment.
Something went wrong with that request. Please try again.