# Admin Chained Filters based on Foreign Keys

**URL:** <https://forum.djangoproject.com/t/admin-chained-filters-based-on-foreign-keys/38581>\
**Category:** The Admin\
**Created:** [February 5, 2025, 2:46pm UTC](https://forum.djangoproject.com/t/admin-chained-filters-based-on-foreign-keys/38581 "2025-02-05T14:46:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![diecavi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/diecavi/32/21560_2.png) [@diecavi](https://forum.djangoproject.com/u/diecavi)\
**Post date:** [February 5, 2025, 2:46pm UTC](https://forum.djangoproject.com/t/admin-chained-filters-based-on-foreign-keys/38581/1 "2025-02-05T14:46:57Z")

</div>

I have a chain of foreign keys relationships, the amount of choices grow exponentially as the database grows making it unmanageable on Admin module, I need to filter options based on previous selection, I have tried for several hours with no solution, I hope someone can give me a hint. Thank you in advance.

```auto
class Org(models.Model):
    name = models.CharField(max_length=60, unique=True)
    # other properties ...

class Branch(models.Model):
    org = models.ForeignKey(Org, on_delete=models.PROTECT)
    name = models.CharField(max_length=60, unique=True)
    # other properties ...

class Store(models.Model):
    branch = models.ForeignKey(Branch, on_delete=models.PROTECT)
    name = models.CharField(max_length=60, unique=True)
    # other properties ...   

class Product(models.Model):
    store = models.ForeignKey(Store, on_delete=models.PROTECT)
    name = models.CharField(max_length=60, unique=True)
    # cost
    # qty_avalable
    # sale_price
    # other properties ...

```

When Creating/Modifying a Product I get ALL the products of ALL the Stores of ALL the Branches of ALL the Orgs, is it possible to filter Branches depending on Org selection, filter Stores on Branch selection and so on?

---

<div class="post-metadata">

**Author:** ![anefta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anefta/32/10649_2.png) [@anefta](https://forum.djangoproject.com/u/anefta)\
**Post date:** [February 6, 2025, 12:05am UTC](https://forum.djangoproject.com/t/admin-chained-filters-based-on-foreign-keys/38581/2 "2025-02-06T00:05:05Z")

</div>

> [@diecavi](#):
>
> I need to filter options based on previous selection

1. Do you mean in the admin area?
2. What are the queries that you tried?

---

<div class="post-metadata">

**Author:** ![diecavi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/diecavi/32/21560_2.png) [@diecavi](https://forum.djangoproject.com/u/diecavi)\
**Post date:** [February 6, 2025, 12:47am UTC](https://forum.djangoproject.com/t/admin-chained-filters-based-on-foreign-keys/38581/3 "2025-02-06T00:47:12Z")

</div>

1. Yes in the admin area
2. I registered the model in the admin.py, works fine, but I have not found a way to include: Org, Branch & Store as dropdowns and then use them to filter the available options at each level

```auto
@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
    list_display == [field.name for field in Product._meta.fields]
    fields = list_display

```

---

<div class="post-metadata">

**Author:** ![anefta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anefta/32/10649_2.png) [@anefta](https://forum.djangoproject.com/u/anefta)\
**Post date:** [February 6, 2025, 8:46am UTC](https://forum.djangoproject.com/t/admin-chained-filters-based-on-foreign-keys/38581/4 "2025-02-06T08:46:20Z")

</div>

You could create a custom model form, register it in `admin.py`, and use custom JavaScript for dynamic filtering with custom views.

---

<div class="post-metadata">

**Author:** ![diecavi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/diecavi/32/21560_2.png) [@diecavi](https://forum.djangoproject.com/u/diecavi)\
**Post date:** [February 6, 2025, 11:34pm UTC](https://forum.djangoproject.com/t/admin-chained-filters-based-on-foreign-keys/38581/5 "2025-02-06T23:34:35Z")

</div>

Thank you for your time and recommendation, but it scales so quickly that it is not practical, I was expecting maybe there was some Django native way to handle these scenarios in Admin. Probably the best way is to do custom views, selecting just one option on the first one, then with limited results the second one, and so on until the last one gets only its reduced list of alternatives.

---

<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, 12:35am UTC](https://forum.djangoproject.com/t/admin-chained-filters-based-on-foreign-keys/38581/6 "2025-02-07T00:35:23Z")

</div>

> [@diecavi](#):
>
> I was expecting maybe there was some Django native way to handle these scenarios in Admin.

This is completely outside the scope of what the Django Admin is designed to do.

See the first two paragraphs at [The Django admin site | Django documentation | Django](https://docs.djangoproject.com/en/5.1/ref/contrib/admin/#module-django.contrib.admin)

You’re going to be a lot better off doing this in a custom view.

> [@diecavi](#):
>
> Probably the best way is to do custom views, selecting just one option on the first one, then with limited results the second one, and so on until the last one gets only its reduced list of alternatives.

In a way, yes, but what you’re talking about here are referred to as “chained” or “cascade” selects. You can use the Django Select2 package to help this.

It’s basically one view for what you want to do, but with some additional views to support the search feature.

---

<div class="post-metadata">

**Author:** ![diecavi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/diecavi/32/21560_2.png) [@diecavi](https://forum.djangoproject.com/u/diecavi)\
**Post date:** [February 8, 2025, 2:50am UTC](https://forum.djangoproject.com/t/admin-chained-filters-based-on-foreign-keys/38581/7 "2025-02-08T02:50:32Z")

</div>

Seems like that’s the best course of action, I’ll check it out to implement it, a nice new resource at hand, thanks a lot, much appreciated!
