Extends Alain Trinh (http://github.com/Soaa-/)'s django-nested-inlines code to work with the latest version of Django Grappelli.
Patches have been submitted, and repositories forked, but no one likes to use either one. Now, nested inlines are available in an easy-to-install package.
This is still beta-quality software, and certainly has its share of bugs. Use it in production sites at your own risk.
pip install grappelli-nested-inlines
grappelli_nested.admin
contains three ModelAdmin
subclasses to enable
nested inline support: NestedModelAdmin
, NestedStackedInline
, and
NestedTabularInline
. To use them:
- Add
grappelli_nested
to yourINSTALLED_APPS
beforegrappelli
anddjango.contrib.admin
. This is because this app overrides certain admin templates and media. - Run
./manage.py collectstatic
to get the new CSS and Javascript files that come with grappelli-nested-inlines. - Import
NestedModelAdmin
,NestedStackedInline
, andNestedTabularInline
wherever you want to use nested inlines. - On admin classes that will contain nested inlines, use
NestedModelAdmin
rather than the standardModelAdmin
. - On inline classes, use
Nested
versions instead of the standard ones. - Add an
inlines = [MyInline,]
attribute to your inlines and watch the magic happen.
from django.contrib import admin
from grappelli_nested.admin import NestedModelAdmin, NestedStackedInline, NestedTabularInline
from models import A, B, C
class MyNestedInline(NestedTabularInline):
model = C
class MyInline(NestedStackedInline):
model = B
inlines = [MyNestedInline,]
class MyAdmin(NestedModelAdmin):
pass
admin.site.register(A, MyAdmin)
Example:
class MyNestedInline(NestedTabularInline):
model = C
classes = ['grp-lazy']
class MyInline(NestedStackedInline):
model = B
inline_classes = ['grp-collapse grp-open']
class MyAdmin(NestedModelAdmin):
inlines = [MyInline,]
Formset for MyNestedInline model admin will get initialized once it's parent form of MyInline is opened. This saves DOM processing time for large nested formsets.
2. Added inline form events in DOM according to: https://docs.djangoproject.com/en/2.0/ref/contrib/admin/javascript/:
formset:added
, formset:removed
as well as new specific events: formset:initialized
triggered when lazy formset has been initialized.
As Trinh said himself, this package is mainly the work of other developers. I (Vestal) have merely adapted this package to support Django Grappelli (as Trinh says he's taken other developers' patches "and packaged them nicely for ease of use").
Besides Trinh, additional credit goes to:
- Gargamel for providing the base patch on the Django ticket.
- Stefan Klug for providing a fork with the patch applied, and for bugfixes.