: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
perf(api): use exists() instead of count() for existence checks (#68602)
Frank Hamand38f9b220ba7b
diff --git a/posthog/api/organization.py b/posthog/api/organization.pyindex 484fc229e250..255267591f6d 100644--- a/posthog/api/organization.py+++ b/posthog/api/organization.py@@ -66,7 +66,7 @@ class PremiumMultiorganizationPermission(permissions.BasePermission):user.organization is Noneor not user.organization.is_feature_available(AvailableFeature.ORGANIZATIONS_PROJECTS))- and user.organizations.count() >= 1+ and user.organizations.exists()):return Falsereturn Truediff --git a/posthog/api/project.py b/posthog/api/project.pyindex 9b6339944437..e682b6821854 100644--- a/posthog/api/project.py+++ b/posthog/api/project.py@@ -1868,7 +1868,7 @@ class PremiumMultiProjectPermission(BasePermission):if request.data.get("is_demo"):# If we're requesting to make a demo project but the org already has a demo project- if organization.teams.filter(is_demo=True).count() > 0:+ if organization.teams.filter(is_demo=True).exists():return Falsecurrent_non_demo_project_count = organization.teams.exclude(is_demo=True).distinct("project_id").count()diff --git a/posthog/api/team.py b/posthog/api/team.pyindex 48da829f015f..a8918f78de1b 100644--- a/posthog/api/team.py+++ b/posthog/api/team.py@@ -2485,7 +2485,7 @@ class PremiumMultiEnvironmentPermission(BasePermission):if request.data.get("is_demo"):# Allow one demo team per organization- if project.organization.teams.filter(is_demo=True).count() > 0:+ if project.organization.teams.filter(is_demo=True).exists():return Falsereturn Truediff --git a/posthog/models/remote_config.py b/posthog/models/remote_config.pyindex c8ef4d3f5b57..8db919b8f99c 100644--- a/posthog/models/remote_config.py+++ b/posthog/models/remote_config.py@@ -225,7 +225,7 @@ class RemoteConfig(UUIDTModel):config = {"token": team.api_token,"supportedCompression": ["gzip", "gzip-js"],- "hasFeatureFlags": FeatureFlag.objects.filter(team=team, active=True).count() > 0,+ "hasFeatureFlags": FeatureFlag.objects.filter(team=team, active=True).exists(),"captureDeadClicks": bool(team.capture_dead_clicks),"capturePerformance": ({diff --git a/products/early_access_features/backend/api.py b/products/early_access_features/backend/api.pyindex 8a1629d235df..d5554f4673d4 100644--- a/products/early_access_features/backend/api.py+++ b/products/early_access_features/backend/api.py@@ -279,7 +279,7 @@ class EarlyAccessFeatureSerializerCreateOnly(EarlyAccessFeatureSerializer):except FeatureFlag.DoesNotExist:raise serializers.ValidationError("Feature Flag with this ID does not exist")- if feature_flag.features.count() > 0:+ if feature_flag.features.exists():raise serializers.ValidationError(f"Linked feature flag {feature_flag.key} already has a feature attached to it.")diff --git a/products/feature_flags/backend/api/feature_flag.py b/products/feature_flags/backend/api/feature_flag.pyindex 39acae232153..56cc83c473b4 100644--- a/products/feature_flags/backend/api/feature_flag.py+++ b/products/feature_flags/backend/api/feature_flag.py@@ -1776,7 +1776,7 @@ class FeatureFlagSerializer(if "deleted" in validated_data and validated_data["deleted"] is True:# Check for linked early access features- if instance.features.count() > 0:+ if instance.features.exists():raise exceptions.ValidationError("Cannot delete a feature flag that is in use with early access features. Please delete the early access feature before deleting the flag.")