views:

123

answers:

1

I am working on an open source Django time tracking app, Djime, and I'm trying to come up with a more efficient way to produce statistics. So far, we've had some rather long-winded procedural code that would get all TimeSlices for a period, and collate them together in a huge nested list/dictionary mess.

What I'd like to do is to set up a more efficient system – an object or function that would take a QuerySet of TimeSlices and collate them by user, task, and/or day.

Our model looks like this (simplified):

class TimeSlice(models.Model):
    task = models.ForeignKey(Task)
    user = models.ForeignKey(User)
    begin = models.DateTimeField(default=datetime.datetime.now)
    duration = models.PositiveIntegerField(null=True, blank=True) # Num. of seconds
    note = models.TextField(null=True, blank=True)
A: 

Sounds like you want the aggregation functionality which is coming in Django 1.1. It's already available in recent checkouts from trunk.

See here for an explanation.

Daniel Roseman
I'm not sure that would be a great way to do it. As I understand it, each aggregate function would cause another query against the data we already have in the QuerySet. I'd much rather have a more Python code than more database requests, since it's easier to scale Python frontends than database servers.
mikl