2006/7/31, Gábor Farkas <[EMAIL PROTECTED]>:
>
> 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:
> >
> >     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:
> >
>
> no, it's not a bug.
>
> datetime.datetime inherits from datetime.
>

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 // [EMAIL PROTECTED]

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to