Django-filter: count data

Tags: , ,

Does anybody knows how can I use count based on selected value using django_filters


'UserFilter' object has no attribute 'count'

My Reference link

  def search(request):
      user_list = Person.objects.all()
      user_filter = UserFilter(request.GET, queryset=user_list)
      count = user_filter.count() #this will return an error
      return render(request, 'user_list.html', {'filter': user_filter})

    from django.contrib.auth.models import User
    from .models import Person
    import django_filters
    class UserFilter(django_filters.FilterSet):
        class Meta:
            model = Person
            fields = ['category', 'firstname', 'lastname' ]


    {% extends 'base.html' %} 
    {% block content %}
      <form method="get">
          <button type="submit" >Search</button>
      <table class="table table-bordered">
            <th> Lastname</th>
          {% for user in filter.qs %}
              <td>{{ user.firstname }}</td>
              <td>{{ user.lastname }}</td>
              <td>{{ user.category }}</td>
          {% empty %}
              <td colspan="5">No data</td>
          {% endfor %}
    {% endblock %}

I want to count all the list base from data I filtered

enter image description here


You’ll want the count of the resulting queryset, which you can get from the filter’s qs property (as you do in your template!).


count = user_filter.count()


count = user_filter.qs.count()

Source: stackoverflow