title: Best way to filter ListView with drop down form in Django
body: <p>I'm trying to filter a ListView based on Users, using a drop down form.</p>
  
  <p>models.py</p>
  
  <pre><code>class Post(models.Model):
      ...
      author = models.ForeignKey('auth.User', verbose_name="Post Author")
  </code></pre>
  
  <p>views.py</p>
  
  <pre><code>class PostList(ListView):
      model = Post
      context_object_name = 'posts'
  
      def get_queryset(self):
          result = super(PostList, self).get_queryset()
  
          author_filter = self.request.GET.get('author')
          if author_filter:
              result = Post.objects.filter(Q(author__icontains=author_filter))
          return result
  </code></pre>
  
  <p>post_list.html</p>
  
  <pre><code>&lt;form action="" method="get"&gt;
            &lt;select name="author" onchange="this.form.submit();"&gt;
              &lt;option selected="selected" disabled&gt;Select an author&lt;/option&gt;
              {% all_author as authors %}
              {% for author in authors %}
              &lt;option value="{{ author }}"&gt;{{ author }}&lt;/option&gt;
              {% endfor %}
            &lt;/select&gt;
          &lt;/form&gt;
  </code></pre>
  
  <p>I am using a custom template tag to render <code>all_authors</code> and this works fine. When selecting an author, in the urls I can see something is passed (/?author=xxx), but the list is not filtered. </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Based on andi's suggestion I made it work this way using django filters. But for some reason <code>fields = ['field_name',]</code> in filters.py is not taken into account, so I'm selecting the fields individually in the template.</p>
  
  <p>views.py</p>
  
  <pre><code>class PostList(FilterView):
      model = Post
      filter_class = PostFilter
      context_object_name = 'posts'
      paginate_by = 50
      template_name = 'directory/post_list.html'
  </code></pre>
  
  <p>filters.py </p>
  
  <pre><code>class PostFilter(django_filters.FilterSet):
  
      class Meta:
          model = Post
          fields = ['author',]
  </code></pre>
  
  <p>post_list.html</p>
  
  <pre><code>&lt;form action="" method="get"&gt;
              {{ filter.form.author }}
              &lt;input type="submit" /&gt;
          &lt;/form&gt;
  </code></pre>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>I've found out why the selected fields were not passed correctly, needed to use in views <code>filterset_class =</code> instead of <code>filter_class =</code></p>