让我们看一下这个例子:
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' ...)
...
对于管理员,让我们ChapterAdmin
像这样配置:
class ChapterAdmin(admin.ModelAdmin):
list_filter = ('book__author', 'book',)
...
这通过相关Author
和提供了很好的过滤器Book
。
问题来了:
当用户点击一个Author
inbook__author
过滤器时,主表中的结果会被相应地过滤。好的。但是book
过滤器仍然列出所有可用Books
的,不管book_author
过滤器。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?