在同一模板上返回过滤器

时间:2014-12-20 01:40:49

标签: django filter django-template-filters

我的views.py中有一个特殊的查询集 我想在下面提到的同一个ListView中退回供应不足的产品 回顾初始它返回所有记录 当我点击链接'产品短缺'是的,它会过滤页面。

我问:

  1. 如何将两个视图保持正常并将过滤器保存在同一个ListView下面?

  2. 如何填写以下链接?

  3. views.py

    from django.views.generic import CreateView, TemplateView, ListView, DetailView
    from django.db.models import F
    from .models import Customer, Brand, Product, Sale, SaleDetail
    
    
    class ProductList(ListView):
        template_name = 'product_list.html'
        model = Product
        context_object = 'product_list'
        paginate_by = 100
    
    
        def get_stock_down(self):
            s = Product.objects.filter(stoq__lt=F('stoq_min'))
            var_get_filter = self.request.GET.get('filter_link')
            return s
    

    product_list.html

    <p name="filter_link" class="pull-right"><a href="">Produtos em baixo estoque</a></p>
    

1 个答案:

答案 0 :(得分:1)

我认为你几乎拥有它。

我认为您应该覆盖get_queryset

,而不是在视图中添加新方法
def get_queryset(self):
    s = Product.objects.all()
    if self.request.GET.get('filter_link', False):
        s = s.filter(stoq__lt=F('stoq_min'))
    return s

<p name="filter_link" class="pull-right"><a href="?filter_link=1">Produtos em baixo estoque</a></p>