使用django count或values_list计数器,哪个更好?

时间:2013-05-11 15:37:51

标签: python django django-queryset

我写了一个导出数据的视图,我的模型是这样的:

class Event(models.Model):
    KIND_CHOICES = (('doing', 'doing'),
                    ('done', 'done'),
                    ('cancel', 'cancel'))
    created_at = models.DateTimeField(auto_now_add=True)
    created_by = models.ForeignKey('auth.User')
    kind = models.CharField(max_length=20, choices=KIND_CHOICES)

事件是三种类型中的一种,每个用户每月可能有3~10个事件,首先我查询本月的事件:

events_this_month = Event.objects.filter(created_at__year=2013,
                                         created_at__month=5)

然后找到所有用户:

users = User.objects.all()

我导出这样的数据:

for user in users:
    # 1000 users with 5 events each
    user_events = events_this_month.filter(created_by=user)
    doing_count = user_events.filter(kind='doing').count()
    done_count = user_events.filter(kind='done').count()
    cancel_count = user.events.filter(kind='cancel').count()

    append_to_csv([user.username, doing_count, done_count, cancel_count])

然后我尝试使用collections.Counter,我认为这将减少计算SQL次数(实际上它从3000 +减少到1200):

for user in users:
    user_events = events_this_month.filter(created_by=user)
    counter = Counter(user_events.values_list('kind', flat=True))
    doing_count = counter['doing']
    done_count = counter['done']
    cancel_count = counter['cancel']

    ...

哪种方式更好

更像是惯用惯用的方式更像是惯用

1 个答案:

答案 0 :(得分:1)

这未经过测试,但我们的想法是按user进行分组,然后按kind进行分组:

from django.db.models import Count

events_this_month = Event.objects.values('created_by', 'kind') \
                         .filter(created_at__year=2013, created_at__month=5) \
                         .annotate(cc=Count('kind'))

让我知道这是否有效,因为我没有测试过这个。