Skip to content

Fix compatibility with django-polymorphic 4.6 (complete) - #280

Open
djmaze wants to merge 1 commit into
theatlantic:masterfrom
djmaze:fix/django-polymorphic-4.6-compatibility
Open

Fix compatibility with django-polymorphic 4.6 (complete)#280
djmaze wants to merge 1 commit into
theatlantic:masterfrom
djmaze:fix/django-polymorphic-4.6-compatibility

Conversation

@djmaze

@djmaze djmaze commented Jan 30, 2026

Copy link
Copy Markdown

As the author of #279 is not responsive, here is a new PR which should fix the remaining problems.

@nattyg93

Copy link
Copy Markdown

@fdintino Any chance we can get this merged and released? With Django 4.2 reaching EOL, this is a blocker for upgrading to Django 5.2 when using django-polymorphic and django-nested-admin.

@fdintino

Copy link
Copy Markdown
Member

The reason I haven't merged this yet is that the logic here is very different than what was in _get_inheritance_relation_fields_and_models.

https://github.com/jazzband/django-polymorphic/blob/d2f9ca27c0bd0ca522e80266f5205a760d7f74d2/src/polymorphic/models.py#L342-L390

I would be more inclined to merge this if the logic was functionally identical. At the very least it needs to be using __subclasses__ since this variable is meant to include child models only. The model parents are already accounted for in a different variable. I think this might be functionally equivalent:

model_cls = self.formset.model.__class__
sub_models = [
    sub_cls
    for sub_cls in model_cls.__subclasses__()
    if model_cls in sub_cls._meta.parents
]

@djmaze
djmaze force-pushed the fix/django-polymorphic-4.6-compatibility branch from 254b04d to ac837ce Compare April 15, 2026 14:47
@djmaze

djmaze commented Apr 15, 2026

Copy link
Copy Markdown
Author

The reason I haven't merged this yet is that the logic here is very different than what was in _get_inheritance_relation_fields_and_models.

@fdintino Okay, sorry, I was not knowledgeable in this codebase. I just tried to implement your suggested approach, maybe it is more correct now?

@nattyg93

nattyg93 commented Jun 4, 2026

Copy link
Copy Markdown

@fdintino not to harass you, but think you could review this one again, please?

@djmaze

djmaze commented Jun 16, 2026

Copy link
Copy Markdown
Author

Interestingly, with Django 6, this seems not a problem anymore.

@theskumar

Copy link
Copy Markdown

@djmaze It's not actually Django 6 that determines this — the trigger is the django-polymorphic version, not the Django version.

_get_inheritance_relation_fields_and_models was removed in django-polymorphic 4.11.0 and is still absent on the current default branch (latest release v4.11.5). The two call sites in nested_admin/polymorphic.py (get_polymorphic_related_models and inline_formset_data) still invoke it, so any polymorphic model with MTI child inlines raises:

AttributeError: '<Model>' object has no attribute '_get_inheritance_relation_fields_and_models'

I just reproduced this on Django 6.0.6 + django-polymorphic 4.11.5 + django-nested-admin 4.1.6 — opening the admin add page for a PolymorphicModel with NestedStackedPolymorphicInline children returns a 500 via inline_formset_dataget_compatible_parentsget_polymorphic_related_models. If a Django-6 setup doesn't hit it, it's almost certainly resolving an older polymorphic (≤4.1, where the method still exists) or has no MTI child models to enumerate.

So this PR is still needed. fdintino's suggested __subclasses__-based logic matches the original method's child-model semantics and is correct. As a sanity check, restoring just that data path (subclasses filtered by model_cls in sub_cls._meta.parents) resolves the 500 and correctly enumerates all child types in our app.

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.

4 participants