On Nov 10, 2015 11:53 PM, "Daniel Sears" <daniel.se...@gmail.com> wrote:
>
> I want to create a list view that searches a large dataset. But it seems
that if I use a filterset with empty defaults, then my view displays my
entire dataset.
>
> If tried creating a get_queryset method in my view that detects whether
my filter fields are empty, but this doesn't seem to have any effect:
>
> from django_filters.views import FilterView
> from .filters import ProductFilter
>
> class ProductView(FilterView):
>     filterset_class = ProductFilter
>     template_name = 'product_filter.html'
>
>     def get_queryset(self):
>         qs = super(ProductView, self).get_queryset()
>         if '' not in self.request.GET.items():
>             return qs.none()
>         else:
>             return qs
>

Yeah, you should probably remove that entire get_queryset() method and let
the FilterView do it's job.

> I looked at and experimented with STRICTNESS, but it didn't seem to have
any effect either. How can I create a view that refrains from displaying
data until I give it valid filter fields?
>
> Thanks.

I'm guessing you are using a third party package that provides FilterView,
although you didn't mention one.

Since I don't know what that inherits from, I'm going to guess ListView,
which uses a *.all() queryset by default. The easiest thing to do would be
to set your initial queryset to *.none() within your class definition,
assuming you are filtering the Product model:

class ProductView(FilterView):
    queryset = Product.objects.none()
   filterset_class = ProductFilter
   template_name = 'product_filter.html'

If that doesn't work (not sure if you can chain filters on top of .none()
and get results, which is probably what FilterView is doing. I'm on my
phone so I can't check), you can also try Product.objects.filter().

-James

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2Be%2BciUd6SGDUCcvn3QsCaSRR99Cj6tsv7cU5yyWvM5Oe9%2BEhA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to