From 9d0ecdaa7df3d2f5053f764e1b152c9f62920ed6 Mon Sep 17 00:00:00 2001 From: Fabian Braun Date: Wed, 30 Oct 2024 23:56:14 +0100 Subject: [PATCH] fix: Show latest title of pages (admin_manager) --- djangocms_link/admin.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/djangocms_link/admin.py b/djangocms_link/admin.py index 7504d732..0410fda2 100644 --- a/djangocms_link/admin.py +++ b/djangocms_link/admin.py @@ -2,7 +2,7 @@ from django.conf import settings from django.contrib import admin from django.core.exceptions import FieldError, PermissionDenied -from django.db.models import Q +from django.db.models import OuterRef, Q, Subquery from django.http import JsonResponse from django.urls import path from django.views.generic.list import BaseListView @@ -98,7 +98,7 @@ def serialize_result(self, obj): """ return { "id": f"{obj._meta.app_label}.{obj._meta.model_name}:{obj.pk}", - "text": str(obj), + "text": getattr(obj, "__link_text__", str(obj)), "url": obj.get_absolute_url(), } @@ -108,16 +108,18 @@ def get_queryset(self): # django CMS 4.2+ qs = PageContent.admin_manager.filter(language=self.language).filter( Q(title__icontains=self.term) | Q(menu_title__icontains=self.term) - ).current_content().values_list("page_id", flat=True) - qs = Page.objects.filter(pk__in=qs).order_by("path") + ).current_content() + qs = (Page.objects.filter(pk__in=qs.values_list("page_id", flat=True)).order_by("path") + .annotate(__link_text__=Subquery(qs.filter(page_id=OuterRef("pk")).values("title")[:1]))) if self.site: qs = qs.filter(site_id=self.site) except (AttributeError, FieldError): # django CMS 3.11 - 4.1 qs = get_manager(PageContent, current_content=True).filter(language=self.language).filter( Q(title__icontains=self.term) | Q(menu_title__icontains=self.term) - ).values_list("page_id", flat=True) - qs = Page.objects.filter(pk__in=qs).order_by("node__path") + ) + qs = (Page.objects.filter(pk__in=qs.values_list("page_id", flat=True)).order_by("node__path") + .annotate(__link_text__=Subquery(qs.filter(page_id=OuterRef("pk")).values("title")[:1]))) if self.site: qs = qs.filter(node__site_id=self.site) return list(qs)