I have a related problem:

I like to calculate time differences with a custom SQL query to
optimize speed. The db cursor seems to return a DateTimeDelta object
(is that a psycopg class?) which I need to convert to a
datetime.timedelta to interoperate with other time objects in Django.

On the other hand, if I query DateTimeFields with custom SQL, Django
does automatically convert them to datetime.datetime objects.

Is there any particular reason why time differences returned by SQL
queries shouldn't be converted to datetime.timedelta objects by Django?

Here's what I have to do now (better ideas appreciated):

class Event(meta.Model):
    starttime = meta.DateTimeField()
    endtime = meta.DateTimeField()
    def duration_minutes(self):
        cursor = db.cursor()
        cursor.execute("SELECT endtime-starttime FROM myapp_events
WHERE id = %s", [self.id])
    try:
        return cursor.fetchone()[0].minutes
        # fetchone() returns e.g. (<DateTimeDelta object for
'02:54:00.00' at b756efc0>,)
    except:
        return 0.0
    def duration(self):
        from datetime import timedelta
        return timedelta(minutes=self.duration_minutes())


--~--~---------~--~----~------------~-------~--~----~
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