[Solved]-How to access the filtered queryset in django admin.SimpleListFilter


For BookAdmin

class AuthorBestSeller(admin.SimpleListFilter):
    title = 'Best selling authors'
    parameter_name = 'bestseller'

    def queryset(self, request, queryset):
        if self.value():
            return queryset.filter(author_id=self.value())
            return queryset

    def lookups(self, request, model_admin):
        qs = model_admin.get_queryset(request)
        query_attrs = dict([(param, val) for param, val in request.GET.items()])
        qs = qs.filter(**query_attrs)
        for book in qs.filter(is_bestseller=1):  # or might be able to use yeild here
            ret.append((book.author_id, book.author))
        return ret

edit: humm… this won’t quite work either. it will just give you a list of authors with best sellers.

then add to BookAdmin

list_filter = (AuthorBestSeller, )

Leave a comment