Skip to content

fix: return first PolymorphicParentModelAdmin#894

Open
niltonpimentel02 wants to merge 3 commits into
django-commons:mainfrom
niltonpimentel02:863
Open

fix: return first PolymorphicParentModelAdmin#894
niltonpimentel02 wants to merge 3 commits into
django-commons:mainfrom
niltonpimentel02:863

Conversation

@niltonpimentel02
Copy link
Copy Markdown
Member

Hello everyone! Based on @bckohan’s comment, I decided to create a test to reproduce the error and verify the fix as well.

Please let me know if I missed anything. Thanks!

Closes #863

Copilot AI review requested due to automatic review settings May 23, 2026 21:05
@niltonpimentel02 niltonpimentel02 requested a review from bckohan as a code owner May 23, 2026 21:05
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes the admin redirect/parent-admin selection logic in PolymorphicChildModelAdmin so it prefers the first PolymorphicParentModelAdmin found up the model MRO, rather than returning an arbitrary registered root ModelAdmin. This addresses issue #863 where saving a child model could redirect to the wrong (root) changelist.

Changes:

  • Update _get_parent_admin() to scan the model MRO and return the first registered PolymorphicParentModelAdmin, skipping regular ModelAdmin registrations on the root model.
  • Add a regression test ensuring a registered root ModelAdmin is ignored when a PolymorphicParentModelAdmin exists higher in the hierarchy.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/polymorphic/admin/childadmin.py Adjusts _get_parent_admin() to return the first PolymorphicParentModelAdmin in the MRO instead of returning the root model’s admin unconditionally.
src/polymorphic/tests/test_admin.py Adds a test reproducing the root-admin redirect bug and verifying the corrected selection behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/polymorphic/admin/childadmin.py Outdated
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
src/polymorphic/admin/childadmin.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

niltonpimentel02 and others added 2 commits May 23, 2026 19:42
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PolymorphicChildModelAdmin redirects to the root model list view instead of the first PolymorphicParentModelAdmin

3 participants