# Django Admin: Chained Filters Not Updating Based on Foreign Key Selections

**URL:** <https://forum.djangoproject.com/t/django-admin-chained-filters-not-updating-based-on-foreign-key-selections/38665>\
**Category:** The Admin\
**Created:** [February 7, 2025, 6:18pm UTC](https://forum.djangoproject.com/t/django-admin-chained-filters-not-updating-based-on-foreign-key-selections/38665 "2025-02-07T18:18:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leerost](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leerost/32/26657_2.png) [@Leerost](https://forum.djangoproject.com/u/Leerost)\
**Post date:** [February 7, 2025, 6:18pm UTC](https://forum.djangoproject.com/t/django-admin-chained-filters-not-updating-based-on-foreign-key-selections/38665/1 "2025-02-07T18:18:54Z")

</div>

Hey there,

I’ve set up chained filters in Django Admin using foreign keys, but they don’t seem to update dynamically when a parent filter is selected. Has anyone experienced this issue before? What’s the proper way to implement chained filters in Django Admin to ensure they update correctly?

Thanks!

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 7, 2025, 6:20pm UTC](https://forum.djangoproject.com/t/django-admin-chained-filters-not-updating-based-on-foreign-key-selections/38665/2 "2025-02-07T18:20:19Z")

</div>

Welcome @Leerost !

Please post your Models involved with this, and the corresponding ModelAdmin classes.

Side Note: When posting code here, enclose the code between lines of three  
backtick - ` characters. This means you’ll have a line of ```, then your code,  
then another line of ```. This forces the forum software to keep your code  
properly formatted.

---

<div class="post-metadata">

**Author:** ![mardukbp](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mardukbp/32/28402_2.png) [@mardukbp](https://forum.djangoproject.com/u/mardukbp)\
**Post date:** [May 5, 2025, 1:02pm UTC](https://forum.djangoproject.com/t/django-admin-chained-filters-not-updating-based-on-foreign-key-selections/38665/3 "2025-05-05T13:02:03Z")

</div>

One way to achieve this is to define the following filter:

```python
class DependentListFilter(admin.RelatedFieldListFilter):
    def __init__ (self, field, request, params, model, model_admin, field_path):
        super(). __init__ (field, request, params, model, model_admin, field_path)

        query_params = {
            self.dependent_field: id
            for param, id in request.GET.items()
            if param.startswith(self.dependent_field)
        }

        if query_params:
            dep_field = self.dependent_field
            filter = {dep_field + '__in': query_params[dep_field]}
            self.lookup_choices = (
                field.related_model.objects
                .filter(**filter)
                .values_list('id', 'name')
            )

```

Assuming your model has the foreign keys `continent` and `country`, define the filter

```python
class CountryListFilter(DependentListFilter):
    dependent_field = 'continent'

```

Then in the model admin use the filter as follows:

```python
@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
    list_display = ['continent', 'country']
    list_filter = [
        ('continent', admin.RelatedFieldListFilter),
        ('country', CountryListFilter)
    ]

```
