From 8db9e92986696f9f722d229fc8c9f0b5cd778f9b Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Wed, 2 Sep 2020 18:01:41 +0200 Subject: [PATCH] Sqlite does not support order by in subqueries --- apps/api/urls.py | 8 +++++++- apps/note/api/views.py | 7 +++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/apps/api/urls.py b/apps/api/urls.py index 9b4d44de..40d21874 100644 --- a/apps/api/urls.py +++ b/apps/api/urls.py @@ -51,7 +51,10 @@ class UserViewSet(ReadProtectedModelViewSet): filterset_fields = ['id', 'username', 'first_name', 'last_name', 'email', 'is_superuser', 'is_staff', 'is_active', ] def get_queryset(self): - queryset = super().get_queryset().order_by("username") + queryset = super().get_queryset() + # Sqlite doesn't support ORDER BY in subqueries + queryset = queryset.order_by("username") \ + if settings.DATABASES[queryset.db]["ENGINE"] == 'django.db.backends.postgresql' else queryset if "search" in self.request.GET: pattern = self.request.GET["search"] @@ -87,6 +90,9 @@ class UserViewSet(ReadProtectedModelViewSet): ), all=True) + queryset = queryset if settings.DATABASES[queryset.db]["ENGINE"] == 'django.db.backends.postgresql' \ + else queryset.order_by("username") + return queryset diff --git a/apps/note/api/views.py b/apps/note/api/views.py index 9b213025..1ab954c9 100644 --- a/apps/note/api/views.py +++ b/apps/note/api/views.py @@ -1,6 +1,6 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later - +from django.conf import settings from django.db.models import Q from django.core.exceptions import ValidationError from django_filters.rest_framework import DjangoFilterBackend @@ -117,6 +117,9 @@ class ConsumerViewSet(ReadOnlyProtectedModelViewSet): """ queryset = super().get_queryset() + # Sqlite doesn't support ORDER BY in subqueries + queryset = queryset.order_by("username") \ + if settings.DATABASES[queryset.db]["ENGINE"] == 'django.db.backends.postgresql' else queryset alias = self.request.query_params.get("alias", ".*") queryset = queryset.prefetch_related('note') @@ -137,7 +140,7 @@ class ConsumerViewSet(ReadOnlyProtectedModelViewSet): ), all=True) - return queryset.order_by('name').distinct() + return queryset.distinct() class TemplateCategoryViewSet(ReadProtectedModelViewSet):