Skip to content

Commit 51b18d0

Browse files
committed
Replace use of RawSQL with functions.
As Django 4.2 does not group on RawSQL automatically any more.
1 parent f2b0675 commit 51b18d0

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

productions/objshow.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
from datetime import datetime
22
from functools import partial
33
from django.core.paginator import Paginator, InvalidPage
4-
from django.db.models import Q, Max
5-
from django.db.models.expressions import RawSQL
4+
from django.db.models import Q, Max, Min, Case, When, F
5+
from django.db.models.functions import Coalesce
6+
# from django.db.models.expressions import RawSQL
67
from django.http import Http404
78
from django.shortcuts import render
89
from .models import Production, Part, ProductionCompany
910
from plays.models import Play
1011
from places.models import Place
1112
from people.models import Person
1213
from aggregates import Concatenate
14+
from fields import ApproximateDateField
1315

1416

1517
# object is Place, Person, Play, or ProductionCompany
@@ -33,10 +35,13 @@ def productions_filter(object, type, date_filter):
3335
else:
3436
raise Exception('Strange call to productions_filter')
3537

38+
end_date_field = f"{annotate_extra}end_date"
39+
press_date_field = f"{annotate_extra}press_date"
40+
start_date_field = f"{annotate_extra}start_date"
3641
o = o.annotate(
37-
max_end_date=Max(annotate_extra + 'end_date'),
38-
max_press_date=Max(annotate_extra + 'press_date'),
39-
max_start_date=Max(annotate_extra + 'start_date')
42+
max_end_date=Max(end_date_field),
43+
max_press_date=Max(press_date_field),
44+
max_start_date=Max(start_date_field)
4045
)
4146
filter = (~Q(max_end_date='') & Q(max_end_date__lt=now)) | Q(max_end_date='', max_press_date__lt=now) | Q(max_end_date='', max_press_date__isnull=True, max_start_date__lt=now)
4247
if filter_extra:
@@ -54,9 +59,10 @@ def productions_filter(object, type, date_filter):
5459
o = o.annotate(part__role__concatenate=Concatenate('part__role', distinct=True))
5560

5661
if date_filter == 'past':
57-
return o.annotate(best_date=RawSQL('MIN(IFNULL(productions_place.press_date, IF(productions_place.end_date!="", productions_place.end_date, productions_place.start_date)))', ())).order_by('-best_date')
62+
return o.annotate(best_date=Min(Coalesce(press_date_field, Case(When(**{end_date_field: ""}, then=F(start_date_field)), default=F(end_date_field)), output_field=ApproximateDateField()))).order_by('-best_date')
63+
# return o.annotate(best_date=Min(RawSQL('IFNULL(productions_place.press_date, IF(productions_place.end_date!="", productions_place.end_date, productions_place.start_date))', ()))).order_by('-best_date')
5864
else:
59-
return o.order_by(annotate_extra + 'start_date', annotate_extra + 'press_date')
65+
return o.order_by(start_date_field, press_date_field)
6066

6167

6268
def _prods_one_way(fltr, object, sort_order):

profiles/views.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
from django.contrib.auth.views import LoginView as DjangoLoginView
77
from django.shortcuts import render
88

9-
from django.db.models import Q, Min
10-
from django.db.models.expressions import RawSQL
9+
from django.db.models import Q, Min, Case, When, F
10+
from django.db.models.functions import Coalesce
11+
# from django.db.models.expressions import RawSQL
1112
from django.contrib.contenttypes.models import ContentType
1213
from django_comments.models import Comment
1314
from django.contrib import messages
@@ -17,6 +18,7 @@
1718
from utils import int_to_base32, base32_to_int
1819
from reversion.models import Revision
1920
from .models import User
21+
from fields import ApproximateDateField
2022

2123

2224
@login_required
@@ -47,7 +49,8 @@ def profile(request, username):
4749
latest.append(versions)
4850

4951
# Min bit isn't needed except to make sure the GROUP BY gets added
50-
seen = user.visit_set.annotate(min_press_date=Min('production__place__press_date')).annotate(best_date=RawSQL('MIN(IFNULL(productions_place.press_date, IF(productions_place.end_date!="", productions_place.end_date, productions_place.start_date)))', ())).order_by('-best_date')
52+
seen = user.visit_set.annotate(min_press_date=Min('production__place__press_date')).annotate(best_date=Min(Coalesce("production__place__press_date", Case(When(production__place__end_date="", then=F("production__place__start_date")), default=F("production__place__end_date")), output_field=ApproximateDateField()))).order_by('-best_date')
53+
# seen = user.visit_set.annotate(min_press_date=Min('production__place__press_date')).annotate(best_date=Min(RawSQL('IFNULL(productions_place.press_date, IF(productions_place.end_date!="", productions_place.end_date, productions_place.start_date))', ()))).order_by('-best_date')
5154

5255
return render(request, 'profile.html', {
5356
'view': user,

0 commit comments

Comments
 (0)