:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(admin): search GroupTypeMapping by team and organization name (#59604)
Xanderb0467aebd53b
diff --git a/posthog/admin/admins/group_type_mapping_admin.py b/posthog/admin/admins/group_type_mapping_admin.pyindex a2680b24aad1..7601a74e976c 100644--- a/posthog/admin/admins/group_type_mapping_admin.py+++ b/posthog/admin/admins/group_type_mapping_admin.py@@ -1,6 +1,7 @@import threadingfrom django.contrib import admin+from django.db.models import Qfrom django.urls import NoReverseMatch, reversefrom django.utils.html import format_html@@ -16,10 +17,12 @@ class GroupTypeMappingAdmin(admin.ModelAdmin):"name_singular","name_plural","team_link",+ "organization_link","created_at",)search_fields = ("group_type", "name_singular", "name_plural")fields = (+ "organization_link","team_link","project_link","group_type",@@ -31,6 +34,7 @@ class GroupTypeMappingAdmin(admin.ModelAdmin):"created_at",)readonly_fields = (+ "organization_link","team_link","project_link","group_type",@@ -51,19 +55,46 @@ class GroupTypeMappingAdmin(admin.ModelAdmin):qs = super().get_queryset(request)# `posthog_grouptypemapping` lives in the persons DB while Team/Project/Dashboard# live in the main DB, so Django can't JOIN across them. Prefetch the Team rows- # for the current page from the main DB to avoid N+1 in team_link's list-view use.+ # (with organization for organization_link) from the main DB to avoid N+1.team_ids = list(qs.values_list("team_id", flat=True).distinct()[:1000])- self._request_local.team_cache = Team.objects.in_bulk(team_ids) if team_ids else {}+ teams = Team.objects.filter(id__in=team_ids).select_related("organization") if team_ids else []+ self._request_local.team_cache = {team.id: team for team in teams}return qs+ def get_search_results(self, request, queryset, search_term):+ base_queryset = queryset+ queryset, may_have_duplicates = super().get_search_results(request, queryset, search_term)+ if not search_term:+ return queryset, may_have_duplicates++ # Resolve team/org name matches on the main DB first, then add an IN-filter on+ # the persons-DB queryset – see get_queryset above for the same cross-DB pattern.+ team_ids = list(+ Team.objects.filter(+ Q(name__icontains=search_term) | Q(organization__name__icontains=search_term)+ ).values_list("id", flat=True)[:1000]+ )+ if team_ids:+ queryset = queryset | base_queryset.filter(team_id__in=team_ids)+ may_have_duplicates = True++ return queryset, may_have_duplicates++ def _cached_team(self, team_id: int) -> Team | None:+ cache = getattr(self._request_local, "team_cache", {})+ team = cache.get(team_id)+ if team is None:+ team = Team.objects.filter(pk=team_id).select_related("organization").first()+ if team is not None:+ cache[team_id] = team+ return team+@admin.display(description="Team")def team_link(self, group_type_mapping: GroupTypeMapping) -> str:team_id = group_type_mapping.team_idif not team_id:return "-"- team = getattr(self._request_local, "team_cache", {}).get(team_id)- if team is None:- team = Team.objects.filter(pk=team_id).only("id", "name").first()+ team = self._cached_team(team_id)if team is None:return f"Team {team_id} (not found)"return format_html(@@ -72,6 +103,20 @@ class GroupTypeMappingAdmin(admin.ModelAdmin):team.name,)+ @admin.display(description="Organization")+ def organization_link(self, group_type_mapping: GroupTypeMapping) -> str:+ team_id = group_type_mapping.team_id+ if not team_id:+ return "-"+ team = self._cached_team(team_id)+ if team is None or team.organization_id is None:+ return "-"+ return format_html(+ '<a href="{}">{}</a>',+ reverse("admin:posthog_organization_change", args=[team.organization_id]),+ team.organization.name,+ )+@admin.display(description="Project")def project_link(self, group_type_mapping: GroupTypeMapping) -> str:project_id = group_type_mapping.project_id