Running a lot of queries
===

I have the following models with its methods:

    class TrendingTopic(models.Model):
     
        categories = models.ManyToManyField('Category', through='TTCategory', blank=True, null=True)
        location = models.ForeignKey(Location)


        def get_rank(self, t_date=None):
            if t_date:
                ttcs = self.trendingtopiccycle_set.filter(cycle_time__gt=t_date)
            else:
                ttcs = self.trendingtopiccycle_set.all()
            if ttcs:
                return sum([ttc.rank for ttc in ttcs])/len(ttcs)
            return 0
        
        def get_day_rank(self,t_date):
            ttcs = self.trendingtopiccycle_set.filter(cycle_time__year=t_date.year,
                                                cycle_time__month=t_date.month,
                                                cycle_time__day=t_date.day)
            sum_rank = sum([ttc.day_rank for ttc in ttcs if ttc.day_rank])
            if sum_rank:
                return sum_rank/len(ttcs)
            return 0 
    

    class TrendingTopicCycle(models.Model):
    
        tt = models.ForeignKey(TrendingTopic)

        cycle_time = models.DateTimeField(default=datetime.now)
        from_tt_before = models.BooleanField(default=False)
        rank = models.FloatField(default=0.0)
        day_rank = models.FloatField(default=0.0)   

And then I have some functions that are used in the views to retrieve the desired information:

- Show the best trending topics of the current day:

        def day_topics(tt_date, limit=10):
        
            tts = [(ttc.tt, ttc.tt.get_day_rank(tt_date)) for ttc in \
                TrendingTopicCycle.objects.distinct('tt__name') \
                .filter(cycle_time__year=tt_date.year,
                        cycle_time__month=tt_date.month,
                        cycle_time__day=tt_date.day)]
            sorted_tts = sorted(tts, key=itemgetter(1), 
                           reverse=True)[:limit]
            return sorted_tts   

- Show the best trending topics of a given location(woeid) within a determined lapse of time:

        def hot_topics(woeid=None, limit=10):

            CYCLE_LIMIT = datetime.now() + relativedelta(hours=-5)
            TT_CYCLES_LIMIT = datetime.now() + relativedelta(days=-2)
            if woeid:
                tts = [ttc.tt for ttc in \
                        TrendingTopicCycle.objects.filter(tt__location__woeid=woeid) \
                        .distinct('tt__name') \
                        .exclude(cycle_time__lt=CYCLE_LIMIT)]
            else:
                tts = [ttc.tt for ttc in \
                        TrendingTopicCycle.objects.distinct('tt__name') \
                        .exclude(cycle_time__lt=CYCLE_LIMIT)]
    
    
            sorted_tts = sorted(tts, key=lambda tt: tt.get_rank(TT_CYCLES_LIMIT),
                        reverse=True)[:limit]    
            return sorted_tts
    

The problem with the current solution is that it runs really slow because its doing a lot of queries(100's) to retrieve the data. I'm using the django debug toolbar to help me measuring the performance.

Obviously I'm doing something terribly wrong and I'm looking for a solution, any help would be greatly appreciated.