def default(self, o):
if isinstance(o, datetime.date):
return o.strftime(self.DATE_FORMAT)
elif isinstance(o, datetime.time):
return o.strftime(self.TIME_FORMAT)
elif isinstance(o, datetime.datetime):
return o.strftime("%s %s" % (self.DATE_FORMAT,
self.TIME_FORMAT))
....
I know that isinstance(o, datetime.date) returns "True" even "o" is a
datetime object. But I don't know - may be it's a python bug? My python
version 2.4.3 from Ubuntu Dapper. So I replace parts of django code and
all works as I want:
def default(self, o):
if isinstance(o, datetime.datetime):
return o.strftime("%s %s" % (self.DATE_FORMAT,
self.TIME_FORMAT))
elif isinstance(o, datetime.time):
return o.strftime(self.TIME_FORMAT)
elif isinstance(o, datetime.date):
return o.strftime(self.DATE_FORMAT)
else:
return super(self, DateTimeAwareJSONEncoder).defaul
I can confirm this behaviour with Debian Testing's Python 2.3.5 too.
--
Jyrki // jyrki.pu...@gmail.com
I think you should make it a ticket and upload your patch.
>
>
> I can confirm this behaviour with Debian Testing's Python 2.3.5 too.
>
--
I like python!
My Blog: http://www.donews.net/limodou
My Django Site: http://www.djangocn.org
NewEdit Maillist: http://groups.google.com/group/NewEdit
no, it's not a bug.
datetime.datetime inherits from datetime.
you can check it like this:
>>> datetime.datetime.__mro__
(<type 'datetime.datetime'>, <type 'datetime.date'>, <type 'object'>)
>>>
(mro is method-resolution-order)
as you see 'datetime' inherits from 'date', which inherits from 'object'.
and if you check the python-help, you'll see that
isinstance "Return whether an object is an instance of a class or of a
subclass thereof".
and because datetime is a subclass of date, isinstance returns True.
maybe you could try this:
if type(o) == type(datetime.datetime):
// your code here
gabor
Definetly not
> you can check it like this:
>
> >>> datetime.datetime.__mro__
> (<type 'datetime.datetime'>, <type 'datetime.date'>, <type 'object'>)
> >>>
>
> (mro is method-resolution-order)
>
> as you see 'datetime' inherits from 'date', which inherits from 'object'.
>
> and if you check the python-help, you'll see that
> isinstance "Return whether an object is an instance of a class or of a
> subclass thereof".
>
> and because datetime is a subclass of date, isinstance returns True.
>
>
> maybe you could try this:
>
> if type(o) == type(datetime.datetime):
> // your code here
>
..but the real problem propably isn't that, it's the built in method
used with json. Because datetime.datetime is a subclass of
datetime.date, it never gets to the part
elif isinstance(o, datetime.datetime):
return o.strftime("%s %s" % (self.DATE_FORMAT,
self.TIME_FORMAT))
instead it gets caught on the very first if. That's why
datetime.datetime comparing propably should be before datetime.date
comparing
--
Jyrki // jyrki.pu...@gmail.com
--
Jyrki // jyrki.pu...@gmail.com
sorry, of course i meant
"datetime.datetime inherits from datetime.time"
gabor
My bad too, since I meant it's definetly not a bug in python ;)
--
Jyrki // jyrki.pu...@gmail.com
Json serialization is now fixed in SVN Trunk version
--
Jyrki // jyrki.pu...@gmail.com