Sqlite does not support order by in subqueries

This commit is contained in:
Yohann D'ANELLO 2020-09-02 18:01:41 +02:00
parent 3e42f4fffb
commit 8db9e92986
2 changed files with 12 additions and 3 deletions

View File

@ -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

View File

@ -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):