Author: russellm
Date: 2007-09-22 08:21:54 -0500 (Sat, 22 Sep 2007)
New Revision: 6406
Modified:
django/trunk/django/utils/encoding.py
django/trunk/tests/modeltests/serializers/models.py
Log:
Fixed #5553 -- Fixed a serialization problem with datetime and time objects.
Thanks to pigletto for the patch.
Modified: django/trunk/django/utils/encoding.py
===================================================================
--- django/trunk/django/utils/encoding.py 2007-09-22 04:58:26 UTC (rev
6405)
+++ django/trunk/django/utils/encoding.py 2007-09-22 13:21:54 UTC (rev
6406)
@@ -1,5 +1,6 @@
import types
import urllib
+import datetime
from django.utils.functional import Promise
class StrAndUnicode(object):
@@ -30,7 +31,7 @@
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__'):
Modified: django/trunk/tests/modeltests/serializers/models.py
===================================================================
--- django/trunk/tests/modeltests/serializers/models.py 2007-09-22 04:58:26 UTC
(rev 6405)
+++ django/trunk/tests/modeltests/serializers/models.py 2007-09-22 13:21:54 UTC
(rev 6406)
@@ -63,6 +63,9 @@
def __unicode__(self):
return self.title
+
+class Score(models.Model):
+ score = models.FloatField()
__test__ = {'API_TESTS':"""
# Create some data:
@@ -83,7 +86,7 @@
>>> 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 @@
# 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 @@
>>> 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
+
"""}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---