Reputation: 12903
Let's take a look at this example:
class Author(models.Model):
...
class Book(models.Model):
author = models.ForeignKey(Author, related_name='books'...)
...
class Chapter(models.Model):
book = models.ForeignKey(Book, related_name='chapters' ...)
...
For the admin, lets configure ChapterAdmin
like so:
class ChapterAdmin(admin.ModelAdmin):
list_filter = ('book__author', 'book',)
...
This gives nice filters by related Author
and Book
.
Here goes the question:
When user clicks on an Author
in book__author
filter, the results in main table get filtered accordingly. Good. But the book
filter still lists all available Books
, regardless of the book_author
filter. Is there a way to make the book
filter dependent of book__author
filter such that when an Author
is selected, only Books
by that are related to the Author
are listed in filter options?
Upvotes: 2
Views: 1258
Reputation: 2481
Looks like you can use SimpleListFilter
to do the job, since you have the request in the lookups
method you can tell which author is selected and provide only a list of his books, more info in the doc: https://docs.djangoproject.com/en/1.4/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter
Upvotes: 3