我写了一个导出数据的视图,我的模型是这样的: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 回答
慕容708150
TA贡献1831条经验 获得超4个赞
这未经测试,但其想法是先分组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'))
让我知道这是否可行,因为我尚未对此进行测试。
添加回答
举报
0/150
提交
取消