Hi all, I've downloaded today a new release of Django and played with json serialization. I found that if you use DateTime field the resulting json string contains only date, but not all datetime. So I viewed a source code of django/core/serializers/json.py and found that:
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:
> 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.
I can confirm this behaviour with Debian Testing's Python 2.3.5 too.
On 7/31/06, Jyrki Pulliainen <jyrki.pulliai...@gmail.com> wrote:
> 2006/7/31, siniy <msi...@gmail.com>: > > 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.
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.
siniy wrote: > Hi all, > I've downloaded today a new release of Django and played with json > serialization. I found that if you use DateTime field the resulting > json string contains only date, but not all datetime. So I viewed a > source code of django/core/serializers/json.py and found that:
> 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:
> siniy wrote: > > Hi all, > > I've downloaded today a new release of Django and played with json > > serialization. I found that if you use DateTime field the resulting > > json string contains only date, but not all datetime. So I viewed a > > source code of django/core/serializers/json.py and found that:
> > 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:
> 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