Fixed #5553 -- Fixed a serialization problem with datetime and time objects. Thanks to pigletto for the patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@6406 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
694323ec74
commit
f2101abacf
|
@ -1,5 +1,6 @@
|
||||||
import types
|
import types
|
||||||
import urllib
|
import urllib
|
||||||
|
import datetime
|
||||||
from django.utils.functional import Promise
|
from django.utils.functional import Promise
|
||||||
|
|
||||||
class StrAndUnicode(object):
|
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 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
|
return s
|
||||||
if not isinstance(s, basestring,):
|
if not isinstance(s, basestring,):
|
||||||
if hasattr(s, '__unicode__'):
|
if hasattr(s, '__unicode__'):
|
||||||
|
|
|
@ -64,6 +64,9 @@ class Movie(models.Model):
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return self.title
|
return self.title
|
||||||
|
|
||||||
|
class Score(models.Model):
|
||||||
|
score = models.FloatField()
|
||||||
|
|
||||||
__test__ = {'API_TESTS':"""
|
__test__ = {'API_TESTS':"""
|
||||||
# Create some data:
|
# Create some data:
|
||||||
>>> from datetime import datetime
|
>>> from datetime import datetime
|
||||||
|
@ -83,7 +86,7 @@ __test__ = {'API_TESTS':"""
|
||||||
>>> a2 = Article(
|
>>> a2 = Article(
|
||||||
... author = joe,
|
... author = joe,
|
||||||
... headline = "Time to reform copyright",
|
... 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.save(); a2.save()
|
||||||
>>> a1.categories = [sports, op_ed]
|
>>> a1.categories = [sports, op_ed]
|
||||||
>>> a2.categories = [music, op_ed]
|
>>> a2.categories = [music, op_ed]
|
||||||
|
@ -181,7 +184,7 @@ __test__ = {'API_TESTS':"""
|
||||||
|
|
||||||
# Serializer output can be restricted to a subset of fields
|
# Serializer output can be restricted to a subset of fields
|
||||||
>>> print serializers.serialize("json", Article.objects.all(), fields=('headline','pub_date'))
|
>>> 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
|
# Every string is serialized as a unicode object, also primary key
|
||||||
# which is 'varchar'
|
# which is 'varchar'
|
||||||
|
@ -207,4 +210,11 @@ u'G\u0119\u015bl\u0105 ja\u017a\u0144'
|
||||||
>>> print list(serializers.deserialize('json', serializers.serialize('json', [mv2])))[0].object.id
|
>>> print list(serializers.deserialize('json', serializers.serialize('json', [mv2])))[0].object.id
|
||||||
None
|
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
|
||||||
|
|
||||||
"""}
|
"""}
|
||||||
|
|
Loading…
Reference in New Issue