From 844cb7746cf0fbc9fbe2ef8772ad2182cc7c0c2e Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Thu, 4 May 2017 12:20:19 +0100 Subject: [PATCH] Add raw SQL fallback for 'last edited pages' query for MySQL --- wagtail/wagtailadmin/views/home.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/wagtail/wagtailadmin/views/home.py b/wagtail/wagtailadmin/views/home.py index 80d2f6be74..bd1b207b79 100644 --- a/wagtail/wagtailadmin/views/home.py +++ b/wagtail/wagtailadmin/views/home.py @@ -2,6 +2,7 @@ from __future__ import absolute_import, unicode_literals from django.conf import settings from django.contrib.auth import get_user_model +from django.db import connection from django.db.models import Max from django.shortcuts import render from django.template.loader import render_to_string @@ -56,10 +57,27 @@ class RecentEditsPanel(object): # Last n edited pages edit_count = getattr(settings, 'WAGTAILADMIN_RECENT_EDITS_LIMIT', 5) - last_edits_dates = (PageRevision.objects.filter(user=self.request.user) - .values('page_id').annotate(latest_date=Max('created_at')) - .order_by('-latest_date').values('latest_date')[:edit_count]) - last_edits = PageRevision.objects.filter(created_at__in=last_edits_dates).order_by('-created_at') + if connection.vendor == 'mysql': + # MySQL can't handle the subselect created by the ORM version - + # it fails with "This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'" + last_edits = PageRevision.objects.raw( + """ + SELECT wp.* FROM + wagtailcore_pagerevision wp JOIN ( + SELECT max(created_at) AS max_created_at, page_id FROM + wagtailcore_pagerevision WHERE user_id = %s GROUP BY page_id ORDER BY max_created_at DESC LIMIT %s + ) AS max_rev ON max_rev.max_created_at = wp.created_at ORDER BY wp.created_at DESC + """, [ + User._meta.pk.get_db_prep_value(self.request.user.pk, connection), + edit_count + ] + ) + else: + last_edits_dates = (PageRevision.objects.filter(user=self.request.user) + .values('page_id').annotate(latest_date=Max('created_at')) + .order_by('-latest_date').values('latest_date')[:edit_count]) + last_edits = PageRevision.objects.filter(created_at__in=last_edits_dates).order_by('-created_at') + page_keys = [pr.page_id for pr in last_edits] pages = Page.objects.specific().in_bulk(page_keys) self.last_edits = [