mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-09-02 16:05:29 +00:00
[Update] 优化sql, or方式改为union (#3682)
* [Update] 优化sql, or方式改为union * [Update] 优化union操作,直接union后,queryset 的一些参数不能使用,如annoate, 如filter assets__isnull=True
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
#
|
||||
import re
|
||||
from django.shortcuts import reverse as dj_reverse
|
||||
from django.db.models import Subquery, QuerySet
|
||||
from django.conf import settings
|
||||
from django.utils import timezone
|
||||
|
||||
@@ -35,3 +36,16 @@ def date_expired_default():
|
||||
years = 70
|
||||
return timezone.now() + timezone.timedelta(days=365*years)
|
||||
|
||||
|
||||
def union_queryset(*args, base_queryset=None):
|
||||
if len(args) == 1:
|
||||
return args[0]
|
||||
elif len(args) == 0:
|
||||
raise ValueError("args is empty")
|
||||
args = [q.order_by() for q in args]
|
||||
sub_query = args[0].union(*args[1:])
|
||||
queryset_id = list(sub_query.values_list('id', flat=True))
|
||||
if not base_queryset:
|
||||
base_queryset = args[0].model.objects
|
||||
queryset = base_queryset.filter(id__in=queryset_id)
|
||||
return queryset
|
||||
|
Reference in New Issue
Block a user