Skip to content

Make related polymorphic object manager selection more robust to multiple inheritance scenarios.#679

Merged
bckohan merged 1 commit intomasterfrom
test-fk-abstract
Dec 7, 2025
Merged

Make related polymorphic object manager selection more robust to multiple inheritance scenarios.#679
bckohan merged 1 commit intomasterfrom
test-fk-abstract

Conversation

@bckohan
Copy link
Collaborator

@bckohan bckohan commented Dec 7, 2025

This PR sets base_model_manager to "objects" if it is None during PolygmorphicModel class creation. This ensures that even if a non-polymorphic Meta class is higher in the MRO of a polymorphic model it doesn't trigger the use of a vanilla Manager for the polymorphic model when it is referenced from another model.

Fixes #437

Replaces #438 - I had trouble force pushing to that branch.

…morphicModel __new__. This

ensures that related objects will use the polymorphic manager even in inheritance scenarios where
non-polymorphic base models are higher in the MRO.

Co-authored-by: Matt Covalt <mcovalt@mailbox.org>
Co-authored-by: Brian Kohan <bckohan@gmail.com>
@codecov
Copy link

codecov bot commented Dec 7, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 75.53%. Comparing base (18be22d) to head (145bf57).
⚠️ Report is 10 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #679      +/-   ##
==========================================
+ Coverage   75.50%   75.53%   +0.03%     
==========================================
  Files          21       21              
  Lines        1343     1345       +2     
  Branches      212      213       +1     
==========================================
+ Hits         1014     1016       +2     
  Misses        257      257              
  Partials       72       72              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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

Copy link
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

This PR fixes issue #437 where foreign keys to polymorphic models could incorrectly resolve to the parent class in multiple inheritance scenarios. The fix ensures that when a polymorphic model inherits from both an abstract non-polymorphic model and PolymorphicModel, the polymorphic manager is correctly set as the base manager for related field queries.

  • Adds automatic setting of base_manager_name to "objects" in PolymorphicModelBase metaclass when it's None
  • Adds a test case using multiple inheritance with an abstract model to verify the fix
  • Updates migrations to reflect that base_manager_name is now set automatically rather than explicitly

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/polymorphic/base.py Adds logic to set base_manager_name to "objects" if None during model class creation, preventing issues with multiple inheritance
src/polymorphic/tests/models.py Introduces RelationAbstractModel and modifies RelationBase to inherit from it, creating a test case for the multiple inheritance scenario
src/polymorphic/tests/test_orm.py Adds .order_by("pk") to test query to ensure deterministic ordering
src/polymorphic/tests/migrations/0001_initial.py Removes explicit base_manager_name settings for RelationBase and its subclasses since it's now set automatically
docs/changelog.rst Documents the fix for issue #437

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

@bckohan bckohan merged commit 11d2b74 into master Dec 7, 2025
91 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Foreign key resolves to parent class when using abstract models

2 participants