From 4f3df4b576fcaa7f2e491144e4ab36c75ceb4022 Mon Sep 17 00:00:00 2001 From: Rohan Rangray Date: Wed, 3 Jun 2015 04:31:48 -0700 Subject: [PATCH] Optimized the query by doing a batch lookup --- flashcards/management/commands/notifyusers.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/flashcards/management/commands/notifyusers.py b/flashcards/management/commands/notifyusers.py index 7d8c436..deec73a 100644 --- a/flashcards/management/commands/notifyusers.py +++ b/flashcards/management/commands/notifyusers.py @@ -1,5 +1,5 @@ from django.core.management import BaseCommand -from flashcards.models import UserFlashcard, Now +from flashcards.models import UserFlashcard, User, Now from django.utils.timezone import now from datetime import timedelta @@ -8,12 +8,14 @@ class Command(BaseCommand): help = 'Notify the users if they have cards to be reviewed' def handle(self, *args, **options): - notify_list = UserFlashcard.objects.prefetch_related('user').filter( + notify_filter = UserFlashcard.objects.prefetch_related('user').filter( next_review__lte=Now() ).exclude( user__registration_id=None, user__last_notified__range=(now()-timedelta(days=1), now()) ).values_list('user').distinct().all() - for user in notify_list: + notify_list = map(lambda x: x[0], notify_filter) + + for user in User.objects.filter(pk__in=notify_list): user[0].notify() -- 1.9.1