mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
Fixed #1443 -- Django's various bits now support dates before 1900. Thanks to SmileyChris, Chris Green, Fredrik Lundh and others for patches and design help
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7946 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -8,6 +8,7 @@ except ImportError:
|
||||
from StringIO import StringIO
|
||||
from django.db import models
|
||||
from django.utils.encoding import smart_str, smart_unicode
|
||||
from django.utils import datetime_safe
|
||||
|
||||
class SerializationError(Exception):
|
||||
"""Something bad happened during serialization."""
|
||||
@@ -59,7 +60,8 @@ class Serializer(object):
|
||||
Convert a field's value to a string.
|
||||
"""
|
||||
if isinstance(field, models.DateTimeField):
|
||||
value = getattr(obj, field.name).strftime("%Y-%m-%d %H:%M:%S")
|
||||
d = datetime_safe.new_datetime(getattr(obj, field.name))
|
||||
value = d.strftime("%Y-%m-%d %H:%M:%S")
|
||||
else:
|
||||
value = field.flatten_data(follow=None, obj=obj).get(field.name, "")
|
||||
return smart_unicode(value)
|
||||
|
||||
@@ -6,6 +6,7 @@ import datetime
|
||||
from django.utils import simplejson
|
||||
from django.core.serializers.python import Serializer as PythonSerializer
|
||||
from django.core.serializers.python import Deserializer as PythonDeserializer
|
||||
from django.utils import datetime_safe
|
||||
try:
|
||||
from cStringIO import StringIO
|
||||
except ImportError:
|
||||
@@ -20,7 +21,7 @@ class Serializer(PythonSerializer):
|
||||
Convert a queryset to JSON.
|
||||
"""
|
||||
internal_use_only = False
|
||||
|
||||
|
||||
def end_serialization(self):
|
||||
self.options.pop('stream', None)
|
||||
self.options.pop('fields', None)
|
||||
@@ -51,9 +52,11 @@ class DjangoJSONEncoder(simplejson.JSONEncoder):
|
||||
|
||||
def default(self, o):
|
||||
if isinstance(o, datetime.datetime):
|
||||
return o.strftime("%s %s" % (self.DATE_FORMAT, self.TIME_FORMAT))
|
||||
d = datetime_safe.new_datetime(o)
|
||||
return d.strftime("%s %s" % (self.DATE_FORMAT, self.TIME_FORMAT))
|
||||
elif isinstance(o, datetime.date):
|
||||
return o.strftime(self.DATE_FORMAT)
|
||||
d = datetime_safe.new_date(o)
|
||||
return d.strftime(self.DATE_FORMAT)
|
||||
elif isinstance(o, datetime.time):
|
||||
return o.strftime(self.TIME_FORMAT)
|
||||
elif isinstance(o, decimal.Decimal):
|
||||
|
||||
Reference in New Issue
Block a user