Skip to content

: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.

fix(growth): stop untouched push inline row breaking org saves (#70047)

Alex Lider48d8fca16d74
diff --git a/products/growth/backend/admin.py b/products/growth/backend/admin.py
index 62f48a9beff5..8df168551c33 100644
--- a/products/growth/backend/admin.py
+++ b/products/growth/backend/admin.py
@@ -4,6 +4,7 @@ from typing import Any
from django import forms
from django.contrib import admin, messages
from django.core.exceptions import ValidationError
+from django.db.models.fields import BLANK_CHOICE_DASH
from django.http import HttpRequest
from django.urls import reverse
from django.utils.html import format_html
@@ -34,6 +35,11 @@ def humanize_product_key(product_key: str) -> str:
return " ".join(parts)
+def product_key_choices() -> list[tuple[str, str]]:
+ """ProductKey values with humanized labels, for the admin dropdowns."""
+ return [(key.value, humanize_product_key(key.value)) for key in sorted(ProductKey, key=lambda k: k.value)]
+
+
class ProductPushCampaignForm(forms.ModelForm):
"""TAM-facing form: product_key constrained to ProductKey at runtime (the model
keeps a plain CharField so the enum can grow without migrations), and rows that
@@ -48,9 +54,7 @@ class ProductPushCampaignForm(forms.ModelForm):
if "product_key" in self.fields:
self.fields["product_key"] = forms.ChoiceField(
# Display humanized names but keep the raw ProductKey as the stored value.
- choices=[
- (key.value, humanize_product_key(key.value)) for key in sorted(ProductKey, key=lambda k: k.value)
- ],
+ choices=product_key_choices(),
label="Product",
help_text="Which product to promote in the org's in-app push card.",
)
@@ -192,6 +196,14 @@ class ProductPushCampaignInlineForm(ProductPushCampaignForm):
model = ProductPushCampaign
exclude = ("organization",)
+ def __init__(self, *args: Any, **kwargs: Any) -> None:
+ super().__init__(*args, **kwargs)
+ # Without a blank choice the add row's <select> submits the first ProductKey,
+ # so every org save inserted a phantom campaign and 500ed once one was queued.
+ product_key_field = self.fields["product_key"]
+ assert isinstance(product_key_field, forms.ChoiceField) # built in ProductPushCampaignForm.__init__
+ product_key_field.choices = BLANK_CHOICE_DASH + product_key_choices()
+
class ProductPushCampaignInline(admin.TabularInline):
"""An organization's push schedule and history on the Organization admin page.
diff --git a/products/growth/backend/tests/test_product_push_admin.py b/products/growth/backend/tests/test_product_push_admin.py
index 0a3dd2b926b2..84716f931f46 100644
--- a/products/growth/backend/tests/test_product_push_admin.py
+++ b/products/growth/backend/tests/test_product_push_admin.py
@@ -2,13 +2,15 @@ from typing import Any
from posthog.test.base import BaseTest
+from django import forms
from django.contrib.admin import AdminSite
+from django.db.models.fields import BLANK_CHOICE_DASH
from django.test import RequestFactory
from django.urls import reverse
from posthog.models.organization import Organization
-from products.growth.backend.admin import ProductPushCampaignAdmin, ProductPushCampaignInline
+from products.growth.backend.admin import ProductPushCampaignAdmin, ProductPushCampaignInline, product_key_choices
from products.growth.backend.models import ProductPushCampaign
@@ -101,3 +103,38 @@ class TestProductPushCampaignAdmin(BaseTest):
assert campaign.status == ProductPushCampaign.Status.SCHEDULED
assert campaign.source == ProductPushCampaign.Source.TAM
assert campaign.created_by == self.user
+
+ def test_untouched_inline_add_row_saves_nothing(self) -> None:
+ # Without a blank first choice, an untouched add row submits the first ProductKey
+ # and inserts a phantom campaign - a 500 once that product is already queued.
+ ProductPushCampaign.objects.create(
+ organization=self.organization, product_key="actions", status=ProductPushCampaign.Status.SCHEDULED
+ )
+ inline = ProductPushCampaignInline(Organization, AdminSite())
+ request = self.request_factory.post("/admin/posthog/organization/")
+ request.user = self.user
+ formset_class = inline.get_formset(request, self.organization)
+ prefix = formset_class.get_default_prefix()
+
+ product_key_field = formset_class.form(instance=None).fields["product_key"]
+ assert isinstance(product_key_field, forms.ChoiceField)
+ assert product_key_field.choices == BLANK_CHOICE_DASH + product_key_choices()
+
+ formset = formset_class(
+ instance=self.organization,
+ data={
+ f"{prefix}-TOTAL_FORMS": "1",
+ f"{prefix}-INITIAL_FORMS": "0",
+ f"{prefix}-MIN_NUM_FORMS": "0",
+ f"{prefix}-MAX_NUM_FORMS": "1000",
+ f"{prefix}-0-id": "",
+ f"{prefix}-0-product_key": "",
+ f"{prefix}-0-position": "0",
+ f"{prefix}-0-scheduled_for": "",
+ f"{prefix}-0-reason_text": "",
+ },
+ )
+
+ assert formset.is_valid(), formset.errors
+ assert formset.save() == []
+ assert ProductPushCampaign.objects.filter(organization=self.organization).count() == 1