Show fields in the filter depending on the query

I use django-filter on my website to search for books. The filter includes the following fields: book genre, language the book is written in, publication date, and search field. The search looks for several fields in the database, including the book title, authors of the book, etc. The filter works fine, but I don't understand how to make it so that when I search for a book when I select some fields - other fields are removed depending on whether there are such objects. For example, I am looking for a book in Chinese, I select this field, and I have the genres not used removed after the results are displayed. Right now I have all the filters shown, even there are no books there The second question is, can I make it so that after I search, I can see how many objects are in each field? That is, if I entered Harry Potter in the search and got a result, I see that there are 5 objects in English.

models.py

class Book(models.Model):
   name = models.CharField(max_length=255)
   author = models.ForeignKey(
        "Authors",
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
    )
   subject = TreeManyToManyField("Subject")
   published_date = models.DateField(blank=True, null=True)
   language = models.ForeignKey(
        "Language", on_delete=models.SET_NULL, null=True)

class Subject(MPTTModel):
   name = models.CharField(
        max_length=1000,
        unique=True,
    )

class Language(models.Model):
    name = models.CharField(
        max_length=255,
        unique=True,
    )

class Authors(models.Model):
       name = models.CharField(
        max_length=255,
        unique=True,
        )

filters.py

class BookFilter(django_filters.FilterSet):
  search = django_filters.CharFilter(
        method="my_custom_filter",
        widget=TextInput(
            attrs={
                "class": "form-control",
                "placeholder": _("Type to search"),
            }
        ),
    )
    language = django_filters.ModelMultipleChoiceFilter(
        field_name="language",
        queryset=Language.objects.order_by("name"),
        widget=forms.CheckboxSelectMultiple(),
    )
    subject = django_filters.ModelMultipleChoiceFilter(
        field_name="subject",
        queryset=Subject.objects.all(),
        widget=autocomplete.ModelSelect2Multiple(}
        ),
    )
    class Meta:
        model = Book
        fields = {
            "subject",
            "language",
            "published_date",
        }

    def my_custom_filter(self, queryset, name, value):
        q = value
        return (
            queryset.annotate(
                rank=SearchRank(vector, q),
                similarity=TrigramSimilarity("title", q)
                + similarity=TrigramSimilarity("author", q)
)

views.py

def BookListView(request):
    book = Book.objects.all()
    filter = BookFilter(request.GET, queryset=book)
    context = {
        "filter": filter,
        "book": book,
    }
    return render(request, "book.html", context)

book.html

{% extends "base.html" %}

{% block content %}
    <form method="get">
        {{ filter.form.as_p }}
        <input type="submit" />
    </form>
    {% for obj in filter.qs %}
        {{ obj.name }}
<br />
    {% endfor %}
{% endblock %}
Back to Top