Philippe Niquille wrote: > Using django I get a QuerySet of Score objects which are sorted by the > actual score, the actual score divided by the max. possible score (so > sorting by two db fields). > > I then need to loop through that queryset and sum up all the score > objects which belong to the same user into one Score objects. This works > (see code below).
I'm with Eric and Alan here; do as much in the DB as possible/practical first, then work with the results in Python. Django's ORM may be able to do your aggregating for you. If not and if your DB has something like PostgreSQL's Views (stored queries) then you can create a database view that filters/sorts/aggregates your data as needed. From there you can create a Django Model associated with the DB View, just like you normally create a Django Model on a DB table. The main difference is that objects returned by the QuerySet on the DB View will be read-only because the DB View is read-only. On the other hand you'll still be able to python-ize your aggregated db data using Django's ORM in a manner you are used to, something like: scores = ModelOnMyDBView.objects.order_by('max_score','owner') I hope that is helpful, Eric. _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor