Admin Chained Filters based on Foreign Keys

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.

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?

  1. Do you mean in the admin area?
  2. What are the queries that you tried?
  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
@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
    list_display == [field.name for field in Product._meta.fields]
    fields = list_display

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

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.

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

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

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.

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!