Let's say we have two objects:
class Author(models.Model): name = models.CharField(length=50) class Book(models.Model): author = models.ForeignKey(Author) is_bestseller = models.BooleanField()
In BookAdmin, if we specify
list_filter = ('author', 'is_bestseller')
the choice provided to you for the "author" will always be for all authors in the database, regardless of whether he wrote a bestseller or not.
I want my filters to be limited to the current selection. I tried to do this in a general way using admin.SimpleListFilter, however I am stuck as:
model_admin.get_queryset()
returns an unfiltered query (i.e. all objects, and not just those that are currently filtered by the user).
How to get a filtered set of requests in BookAdmin?
EDIT: To better illustrate the problem, here is my code:
class ForeignFieldFilter(admin.SimpleListFilter): field = None def lookups(self, request, model_admin): queryset = model_admin.get_queryset(request).select_related(self.field)
django django-admin
Enuy
source share