[Python] How to build chained filter in django admin dashboard

Discussão em 'Python' iniciado por Stack, Maio 5, 2025.

  1. Stack

    Stack Membro Participativo

    I want chained filter in django-admin dashboard,Please help me to achieve it. I have 3 models:- Board, Grade and Chapter. Initially when dashboard loads then only board filter should appear and after selecting board filter the selected boards all filter should appear and after selecting grade all chapters of that grade should appear.

    I have tried RelatedDropdownFilter, but thats just normal, it does not modify the view part of dropdownfilter, It shows all results.

    My models:-

    class LmsBoard(models.Model):
    name = models.CharField(max_length=240)



    class LmsGrade(models.Model):
    name = models.CharField(max_length=240)
    board = models.ForeignKey(LmsBoard, models.DO_NOTHING)


    class LmsSubject(models.Model):
    name = models.CharField(max_length=240)
    grade = models.ForeignKey(LmsGrade, models.DO_NOTHING)
    board = models.ForeignKey(LmsBoard,models.DO_NOTHING)

    class LmsChapter(models.Model):
    name = models.CharField(max_length=240,default='None')
    subject = models.ForeignKey(LmsSubject, models.DO_NOTHING)


    My admin.py:-

    @admin.register(MyModel)
    class MyModel(admin.ModelAdmin):
    list_display = (
    'board', 'grade', 'chapter',)
    list_filter = (
    ('board',RelatedDropdownFilter),
    ('grade', RelatedDropdownFilter),
    ('chapter', RelatedDropdownFilter),


    I am expecting the filter should appear one by one and should contain the filter list of last selectected item.

    Continue reading...

Compartilhe esta Página