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(replay-vision): pass native types to quota-grant admin initial (#62284)

Tue Haulundbeb876aabc5b
diff --git a/products/replay_vision/backend/admin.py b/products/replay_vision/backend/admin.py
index 2f5817f27bd2..9d3e6eff23e7 100644
--- a/products/replay_vision/backend/admin.py
+++ b/products/replay_vision/backend/admin.py
@@ -54,6 +54,8 @@ class ReplayQuotaGrantAdmin(admin.ModelAdmin):
def get_changeform_initial_data(self, request: HttpRequest) -> dict[str, Any]:
initial: dict[str, Any] = super().get_changeform_initial_data(request)
# Pre-fill but don't force — admins can clear either field on the add form.
- initial.setdefault("expires_at", next_month_start(datetime.now(UTC)).isoformat())
- initial.setdefault("granted_by", str(request.user.pk))
+ # Pass native types (not strings): the admin's SplitDateTimeWidget calls
+ # `to_current_timezone(value)` on initial, which AttributeErrors on a str.
+ initial.setdefault("expires_at", next_month_start(datetime.now(UTC)))
+ initial.setdefault("granted_by", request.user.pk)
return initial
diff --git a/products/replay_vision/backend/tests/test_quota.py b/products/replay_vision/backend/tests/test_quota.py
index 6e875a64753d..84556314a711 100644
--- a/products/replay_vision/backend/tests/test_quota.py
+++ b/products/replay_vision/backend/tests/test_quota.py
@@ -224,6 +224,32 @@ class TestQuotaGrants(_VisionQuotaTestCase):
assert snapshot.remaining == 1
+class TestReplayQuotaGrantAdmin(_VisionQuotaTestCase):
+ def test_initial_form_renders(self) -> None:
+ # Regression: `expires_at` initial must be a datetime, not a str. The admin's
+ # SplitDateTimeWidget.decompress runs `to_current_timezone` → `is_aware` →
+ # `value.utcoffset()`, which AttributeErrors on a str and 500s the add page in prod.
+ # We render the form directly instead of GETting /admin/...add/ because the admin
+ # URLs are gated on ADMIN_PORTAL_ENABLED, which defaults False in product test env.
+ from django.contrib import admin as django_admin
+ from django.test import RequestFactory
+
+ from products.replay_vision.backend.admin import ReplayQuotaGrantAdmin
+
+ self.user.is_staff = True
+ self.user.save()
+ request = RequestFactory().get("/admin/replay_vision/replayquotagrant/add/")
+ request.user = self.user
+
+ grant_admin = ReplayQuotaGrantAdmin(ReplayQuotaGrant, django_admin.site)
+ form_class = grant_admin.get_form(request)
+ form = form_class(initial=grant_admin.get_changeform_initial_data(request))
+ # `as_p()` triggers widget render, which is where the bug fires.
+ html = form.as_p()
+ assert 'name="expires_at_0"' in html
+ assert 'name="granted_by"' in html
+
+
class TestVisionQuotaEndpoint(_VisionQuotaTestCase):
@property
def quota_url(self) -> str: