Package: django-cascading-dropdown-widget
django-cascading-dropdown-widgetProvide a cascading-dropdown widget for django.Installpipinstalldjango-cascading-dropdown-widgetUsagepro/settings.pyNote:The application used template of django_cascading_dropdown_widget, so MUST include django_cascading_dropdown_widget in INSTALLED_APPS.INSTALLED_APPS=[...'django_cascading_dropdown_widget',...]app/admin.pyNote:Create a new ModelForm, and setting field widget to DjangoCascadingDropdownWidget.Create a choices generator instance.fromdjango.contribimportadminfromdjango_cascading_dropdown_widget.widgetsimportDjangoCascadingDropdownWidgetfromdjango_cascading_dropdown_widget.widgetsimportCascadingModelchoicesfromdjangoimportformsfrom.modelsimportCategoryfrom.modelsimportBookfrom.modelsimportCharacterclassCharacterForm(forms.ModelForm):classMeta:model=Categoryexclude=[]widgets={"book":DjangoCascadingDropdownWidget(choices=CascadingModelchoices({"model":Category,"related_name":"books",},{"model":Book,"fk_name":"category",})),}classCharacterAdmin(admin.ModelAdmin):form=CharacterFormlist_display=["name","book"]Choices generatorCascadingModelchoicesclassCascadingModelchoices(object):def__init__(self,*cascadings):...The parameters for CascadingModelchoices generator are the CASCADING-MODEL-SETTINGS(name it cascadings for short). A MODEL-SETTING(name it cascading for short)'s config items are:model, required. The Model class.related_name, required but except for the last model-setting. Use the related_name to get the queryset of the next level items.fk_name, required but except for the first model-setting. Which field name to get parent model.empty, optional. Useemptystring instread of '----- xxx ----' in select for empty value.str, optional. Get item title fromstrmethod or property instread of get title bystr(item).SimpleChoicesSimpleChoices2 (alias)classSimpleChoices(object):def__init__(self,*cascadings,empty1="-"*10,empty2="-"*10):...A cascading is a tuple of (Title1, List).The Title1 is the first level option.The List is the second level selector. The List item is a tuple of (Value2, Title2).The Value2 is the second level option's value.The Title2 is the second level option's title.MPTTModel ChoicesUse CascadingModelchoices for the generator.Createindented_titlefunction for the MPTTModel. You use whatever name for theindented_title.class MyModel(MPTTModel):
    def indented_title(self):
        return ("-"*4) * self.get_level() + self.nameSetting"str": "indented_title"in cascading.The application django-cascading-dropdown-widget is NOT required django-mptt, so install django-mptt by youself. We have did try...except... with django-mptt's missing.Releasesv0.2.7 2023/09/14Doc update.v0.2.6 2021/04/08Fix problems that sometimes the init change event is not triggered.v0.2.5 2020/12/01Remove arrive.js deps.Use django's jquery.Fix change event first trigger problem. We should ignore the first trigger.v0.2.2 2020/03/27Use$(document).on("change", ".django-cascading-dropdown-widget-select", function(){...})instead ofarrivemonitor. So now we do NOT depends on django-static-arrive.v0.2.1 2020/03/25Use django-static-arrive js file.Fire change event on the hidden input.v0.2.0 2020/03/23Add SimpleChoices generator.Addwhite-space: nowrap;for django-cascading-dropdown-widget.v0.1.0 2020/03/16Fisrt release.