Skip to content

Polymorphic child model discovery fails for deeply nested or abstract class hierarchies #277

@MBI-joshquercia

Description

@MBI-joshquercia

Problem

The current implementation of NestedPolymorphicInlineAdminFormset.inline_formset_data() uses self.formset.model()._get_inheritance_relation_fields_and_models() to discover child polymorphic models. This method has limitations:

  1. It doesn't discover child models at arbitrary inheritance depths
  2. It doesn't correctly handle abstract polymorphic base classes
  3. It returns deferred model classes in some querysets (when using .only())

Proposed Solution

Replace the child model discovery with two new helper functions:

  • get_all_subclasses(python_class) - Recursively finds all subclasses at any depth
  • get_child_concrete_polymorphic_models(base_model) - Filters to only concrete (non-abstract) polymorphic models, excluding deferred classes

Implementation

I have a working implementation with tests in my fork:
https://github.com/jintb/django-nested-admin

Key changes in nested_admin/polymorphic.py:

  • Added get_all_subclasses() helper function
  • Added get_child_concrete_polymorphic_models() function
  • Updated the call site in NestedPolymorphicInlineAdminFormset.inline_formset_data()

Tests added in nested_admin/tests/nested_polymorphic/test_polymorphic_abstract_classes/

Happy to open a PR if there's interest in this fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions