django-filter页面启动时结果列表为空 [英] Empty result list on django-filter page startup
本文介绍了django-filter页面启动时结果列表为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用
解决方案
之后一些研究,我发现了这个简单的解决方案
class ProductFilter(django_filters.FilterSet):
class Meta:
模型=产品
字段= ['短名称','全名','描述','产品类型',]
def __init __(self,* args,** kwargs):
super(ProductFilter,self ).__ init __(* args,** kwargs)
#在sturtup用户处不按Submit按钮,如果self.data == {}:$ b $,则QueryDict(数据中)为空
b self.queryset = self.queryset.none()
我认为,这种解决方案是稳定的。您如何看待?
I'm using https://github.com/alex/django-filter
When user opens the page with filter at the first time, they see the empty form and full list of results.
I need to not to display the result until user press Search for a first time.
In other words - django-filter should not display any results if parameters list (in url) is empty, and display all results if parameter values (in url) is empty.
Source
filters.py
import django_filters
from product.models import Product
class ProductFilter(django_filters.FilterSet):
class Meta:
model = Product
fields = [ 'shortname', 'fullname', 'description', 'product_type' ]
views.py
from product.filters import ProductFilter
def product_search_in_ancestors(request, product_id):
context = RequestContext(request)
p = get_object_or_404( Product, pk=product_id )
q = Product.objects.filter( id__in = p.GetChilds() )
filter = ProductFilter(request.GET , queryset=q )
return render_to_response( 'product/product_search_in_ancestors.html',
{'product':p,
'filter': filter
},
context)
解决方案
After some studies, I've found this simple solution
class ProductFilter(django_filters.FilterSet):
class Meta:
model = Product
fields = [ 'shortname', 'fullname', 'description', 'product_type', ]
def __init__(self, *args, **kwargs):
super(ProductFilter, self).__init__(*args, **kwargs)
# at sturtup user doen't push Submit button, and QueryDict (in data) is empty
if self.data == {}:
self.queryset = self.queryset.none()
I think, this solution is stable. How do you think?
这篇关于django-filter页面启动时结果列表为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文