Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-discover child models and inlines #582

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f075aab
Raise proper exception if no child models are available
piranna Feb 16, 2024
d9644fa
Merge branch 'jazzband:master' into master
piranna Feb 21, 2024
d12eccf
Auto-discover child models by default
piranna Feb 29, 2024
308f1d4
Auto-discover child inlines by default
piranna Feb 29, 2024
4830b30
Merge remote-tracking branch 'jazzband/master'
piranna Mar 4, 2024
f8a9302
Fix string template
piranna Mar 5, 2024
91b0a47
Merge remote-tracking branch 'jazzband/master'
piranna Mar 27, 2024
87e4b95
Fix string template
piranna Mar 27, 2024
0de8744
Adjust DocStrings to pep8
piranna Mar 27, 2024
fa69bf7
Merge branch 'jazzband:master' into master
piranna Apr 4, 2024
8da75ce
Merge remote-tracking branch 'jazzband/master'
piranna Apr 24, 2024
ada2ef3
Merge remote-tracking branch 'jazzband/master'
piranna May 17, 2024
b4db7e0
Merge remote-tracking branch 'jazzband/master'
piranna May 18, 2024
c97c5c6
Use already initialized `_child_models`
piranna May 19, 2024
7a0f54c
Allow to exclude child subclasses
piranna May 19, 2024
4136bd6
Merge remote-tracking branch 'jazzband/master'
piranna May 19, 2024
d6b2a88
Merge remote-tracking branch 'jazzband/master'
piranna Jun 17, 2024
aaa7fa3
Don't consider `abstract` Models as leaf subclasses
piranna Jun 17, 2024
0f5f302
Replace `childs` for `children`
piranna Sep 2, 2024
b6cac5e
Accept single instance in `exclude`
piranna Sep 2, 2024
49be2ef
Added documentation
piranna Sep 2, 2024
39f1d79
Fix ruff formatting
piranna Sep 2, 2024
515885d
Remove deprecated warning on `ruff` linting command
piranna Sep 2, 2024
b56b3cd
Add `Jesús Leganés-Combarro` to `AUTHORS.rst` file
piranna Sep 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Allow to exclude child subclasses
  • Loading branch information
piranna committed May 19, 2024
commit 7a0f54ca40ad6a596a0761f12ff59e9c5e61db7b
10 changes: 5 additions & 5 deletions polymorphic/admin/helpers.py
Original file line number Diff line number Diff line change
@@ -144,17 +144,17 @@ def get_inline_formsets(self, request, formsets, inline_instances, obj=None, *ar
return inline_admin_formsets


def get_leaf_subclasses(cls):
def get_leaf_subclasses(cls, exclude=None):
"Get leaf subclasses of `cls` class"

result = []

subclasses = cls.__subclasses__()

if not subclasses:
result.append(cls)
else:
if subclasses:
for subclass in subclasses:
result.extend(get_leaf_subclasses(subclass))
result.extend(get_leaf_subclasses(subclass, exclude))
elif not (exclude and cls in exclude):
result.append(cls)

return result
5 changes: 4 additions & 1 deletion polymorphic/admin/inlines.py
Original file line number Diff line number Diff line change
@@ -54,6 +54,7 @@ class PolymorphicInlineModelAdmin(InlineModelAdmin):
#: Inlines for all model sub types that can be displayed in this inline.
#: Each row is a :class:`PolymorphicInlineModelAdmin.Child`
piranna marked this conversation as resolved.
Show resolved Hide resolved
child_inlines = None
exclude_childs = None
piranna marked this conversation as resolved.
Show resolved Hide resolved

def __init__(self, parent_model, admin_site):
super().__init__(parent_model, admin_site)
@@ -91,7 +92,9 @@ def get_child_inlines(self):
if self.child_inlines is not None:
return self.child_inlines

child_inlines = get_leaf_subclasses(PolymorphicInlineModelAdmin.Child)
child_inlines = get_leaf_subclasses(
PolymorphicInlineModelAdmin.Child, self.exclude_childs
)
child_inlines = tuple(
inline
for inline in child_inlines
5 changes: 4 additions & 1 deletion polymorphic/admin/parentadmin.py
Original file line number Diff line number Diff line change
@@ -51,6 +51,7 @@ class PolymorphicParentModelAdmin(admin.ModelAdmin):

#: The child models that should be displayed
child_models = None
exclude_childs = None
piranna marked this conversation as resolved.
Show resolved Hide resolved

#: Whether the list should be polymorphic too, leave to ``False`` to optimize
polymorphic_list = False
@@ -121,7 +122,9 @@ def get_child_models(self):
if self.child_models is not None:
return self.child_models

child_models = get_leaf_subclasses(self.base_model)
child_models = get_leaf_subclasses(
self.base_model, self.exclude_childs
)

if child_models:
return child_models