3

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

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']

    ...

哪种方式更好

哪里有更惯用的方法来更有效地计算这样的数据?

4

1 回答 1

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'))

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

于 2013-05-11T15:49:06.050 回答