From f955c35f2d53ebd518f0b31d0e22231fe1cbd7b1 Mon Sep 17 00:00:00 2001 From: Tobias Kunze Date: Tue, 27 Sep 2016 11:42:58 +0200 Subject: [PATCH] Fix voucher tag overview with MySQL (closes #253) (#254) This is a fix for this MySQL issue: https://code.djangoproject.com/ticket/24662 It is (imo) the best solution over custom SQL queries or explicit int casting later on. --- src/pretix/control/views/vouchers.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/pretix/control/views/vouchers.py b/src/pretix/control/views/vouchers.py index 567e079fd..abaa067e0 100644 --- a/src/pretix/control/views/vouchers.py +++ b/src/pretix/control/views/vouchers.py @@ -5,7 +5,7 @@ from django.conf import settings from django.contrib import messages from django.core.urlresolvers import resolve, reverse from django.db import transaction -from django.db.models import Count, Q, Sum +from django.db.models import Case, Count, IntegerField, Q, Sum, When from django.http import ( Http404, HttpResponse, HttpResponseBadRequest, HttpResponseRedirect, JsonResponse, @@ -97,7 +97,14 @@ class VoucherTags(EventPermissionRequiredMixin, TemplateView): tags = self.request.event.vouchers.order_by('tag').filter(tag__isnull=False).values('tag').annotate( total=Count('id'), - redeemed=Sum('redeemed') + # This is a fix for this MySQL issue: https://code.djangoproject.com/ticket/24662 + redeemed=Sum( + Case( + When(redeemed=True, then=1), + When(redeemed=False, then=0), + output_field=IntegerField() + ) + ) ) for t in tags: t['percentage'] = int((t['redeemed'] / t['total']) * 100)