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:
- It doesn't discover child models at arbitrary inheritance depths
- It doesn't correctly handle abstract polymorphic base classes
- 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.
Problem
The current implementation of
NestedPolymorphicInlineAdminFormset.inline_formset_data()usesself.formset.model()._get_inheritance_relation_fields_and_models()to discover child polymorphic models. This method has limitations:.only())Proposed Solution
Replace the child model discovery with two new helper functions:
get_all_subclasses(python_class)- Recursively finds all subclasses at any depthget_child_concrete_polymorphic_models(base_model)- Filters to only concrete (non-abstract) polymorphic models, excluding deferred classesImplementation
I have a working implementation with tests in my fork:
https://github.com/jintb/django-nested-admin
Key changes in
nested_admin/polymorphic.py:get_all_subclasses()helper functionget_child_concrete_polymorphic_models()functionNestedPolymorphicInlineAdminFormset.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.