Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions src/polymorphic/admin/parentadmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,17 +234,6 @@ def delete_view(self, request, object_id, extra_context=None):
real_admin = self._get_real_admin(object_id)
return real_admin.delete_view(request, object_id, extra_context)

def get_preserved_filters(self, request):
if "_changelist_filters" in request.GET:
request.GET = request.GET.copy()
filters = request.GET.get("_changelist_filters")
f = filters.split("&")
for x in f:
c = x.split("=")
request.GET[c[0]] = c[1]
del request.GET["_changelist_filters"]
return super().get_preserved_filters(request)

def get_urls(self):
"""
Expose the custom URLs for the subclasses and the URL resolver.
Expand Down
39 changes: 39 additions & 0 deletions src/polymorphic/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,3 +521,42 @@ def test_admin_recent_actions(self):
assert values == ["2D1", "2D2", "2D4"]
else:
assert False, f"Unexpected change url: {action_url}"


class AdminPreservedFiltersTests(_GenericAdminFormTest):
def test_changelist_filter_persists_after_edit(self):
# Arrange: create 1 instance for each concrete polymorphic child model
# so the changelist has something to filter and something to click into.
Model2B.objects.create(field1="B1", field2="B2")
Model2C.objects.create(field1="C1", field2="C2", field3="C3")

# Get the ContentType for Model2B. The admin filter uses this PK to
# restrict the polymorphic changelist to only "B" rows.
ct_b = ContentType.objects.get_for_model(Model2B)

# Build a changelist URL for the polymorphic parent (Model2A) with the
# polymorphic content-type filter applied via querystring.
filtered_url = f"{self.list_url(Model2A)}?polymorphic_ctype__id__exact={ct_b.pk}"

# Act: open the filtered changelist page in the browser.
self.page.goto(filtered_url)

# Click the first row's object link in the results table to go to its change form.
self.page.click("table#result_list tbody tr th a")

# Edit a field on the change form.
self.page.fill("input[name='field1']", "B1-edited")

# Click Save and explicitly wait for navigation caused by form submission.
# Capturing the navigation response lets us assert the HTTP status.
with self.page.expect_navigation(timeout=10000) as nav_info:
self.page.click("input[name='_save']")
response = nav_info.value

# Assert: request succeeded (admin returned a normal page load).
expected_status_code = 200
assert response.status == expected_status_code

# Assert: after saving, the redirected URL still contains the original filter,
# meaning the changelist preserved the querystring across edit/save.
assert f"polymorphic_ctype__id__exact={ct_b.pk}" in self.page.url
Loading