'Get a result and the filter only has an associated field [Django-filter]

I want to create django-filter where when users search for a book by title or content they get a result and the filter only has an associated field. And also in each field there is a number of containing books with numbers. For example,I'm looking for a book about nature, I write "nature" in the search field in the filter, and then I click "Search". I get a result and a filter with the corresponding fields and results (for example, the filter has the following values - language filter: English (54), German (20) ; Publisher: Oxford (100), Seven House(21) etc.) I want to create a django filter where when users search for a book by title or content they get a result and the filter only has an associated field. And also in each field there is a number of containing books with numbers. For example, I'm looking for a book about nature, I write "nature" in the search field in the filter, and then I click "Search". I get a result and a filter with the corresponding fields and results (for example, the filter has the following values ​​- language filter: English (54), German (20) ; Publisher: Oxford (100), Seven House(21), etc.) How to summarize my main questions

  1. How to create a filter that after the request had values ​​relevant to the request?
  2. How to count this data in the filter?

models.py

class Book(models.Model):
   title = models.CharField(max_length=255)
   description = models.CharField(max_length=255)
   publisher = models.ForeignKey("Publisher", null=True, blank=True, on_delete=models.SET_NULL)
   language = models.ForeignKey("Language", null=True, blank=True, on_delete=models.SET_NULL)

class Publisher(models.Model):
    name = models.CharField(max_length=255)

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

books.hmtl

<div>
 {{filter.form.as_p}}
 {{filter.form.media}}
</div>

<div>
{% for book in filter.qs %}
    {{book.title}}
    {{book.description}}
{% endfor %}
</div>

filters.py

class BookFilter(django_filters.FilterSet):
    class Meta:
        model = Book
        fields = {
            "title",
            "description",
            "language",
            "publisher",
        }

views.py

def BookListView(request):

    book = Book.objects.all()
    filter = BookFilter(request.GET, queryset=books)

    context = {"books": books, "filter": filter}
    return render(request, "books.html", context)




Solution 1:[1]

As per my understanding, you have three requirements.

  1. To get all the books that contain searched titles.

    books = Books.objects.filter(title__contains='nature').select_related('publisher', 'language')
    
  2. Get the list of books count with respect to the language.

    books.values('language__name').annotate(count=Count('id'))
    
  3. Get the list of books count with respect to the publisher.

    books.values('publisher__name').annotate(count=Count('id'))
    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 B.Anup