使用排序&在QuerySet上进行过滤 [英] Using both sort & filter on a QuerySet

查看:198
本文介绍了使用排序&在QuerySet上进行过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我想要排序和过滤的用户文件列表。



我已经能够手动输入URL,但是我无法对模板页面进行编码,以允许先前持续的应用过滤器或排序。以下是我目前拥有的网址和模板代码 -

 #在urls 
url(r'^ talent / filter \(?P< position> [A-Za-z] +)/ sort\ :( :( P< sort> [A-Za-z] +)$','talent_filter',name =' talent_filter_sort'),
url(r'^ talent / filter\(?P< position> [A-Za-z] +)/ $','talent_filter',name ='talent_filter'),
url(r'^ talent / sort\ :( :( P< sort> [A-Za-z] +)/ $','talent_sort',name ='talent_sort'),
url r'^ talent / $','talent',name ='talent'),

#在模板
< ul>
< li> SORT BY:< / li>
< li>< a href ={%url talent_sort sort ='alphabetical'%}>字母顺序< / a>< / li>
...
< / ul>
< ul>
< li>< a href ='{%url talent_filter position = position%}'> {{position}}< / a>< / li>
...
< / ul>

目前,如果我在(未排序,未过滤的)人才页面,而且我选择一个过滤器结果,它将返回
talent / filter:filter 。然后,如果我选择对结果进行排序,(显然)它会转到 talent / sort:sort ,删除以前的过滤器。



我想要完成的是,如果我目前在 talent / filter:filter 并单击排序方法,它将转到 talent / filter:filter / sort:sort ,如果我已经排序结果( talent / sort:sort ),然后单击过滤器,它也将带我到 talent / filter:filter / sort:sort 。我该如何做到这一点谢谢。

解决方案

我认为你可能能够完成这一点的一种方式是在你的会话中存储一个标志,表示什么排序或过滤应该是。例如,可以使用下面的内容来保存排序选择的状态。

  request.session ['TALANT_SORT' ] =字母
request.session ['TALENT_FILTER'] =top_ten

然后,您的视图可以检查会话密钥的存在并相应地应用过滤器。

  qs = models.MyModel.objects。 all()

sort = request.session.get('TALENT_SORT',None)
如果在[字母,created_date]中排序:
qs = qs。 order_by(sort)

myfilter = request.session.get(TALENT_FILTER,无)
如果myfilter在[top_ten,bottom_ten]中:
qs = qs .filter(....)

....

排序和过滤可以在请求之间持续存在。



如果您要删除排序或过滤器,则可能会根据某些用户操作删除视图中的会话密钥:

  try:
del request.session ['TALENT_SORT']
del request.session ['TALENT_FILTER']
除了KeyError :
pass

另外,根据您的要求,您可以考虑将2个网址合并1,只需使用GET参数来激活排序。

  request.GET.get('sort',None)
....
请求。 GET.get('filter',None)
....

可能使用更严格,但这是想法。希望这可以帮助。
Joe


I have a list of userprofiles that I want to be able to sort and filter.

I have been able to do this manually, by manually typing in the URLs, but I haven't been able to code the template page to allow the persistence of a previously-applied filter or sort. Here is the url and template code that I currently have --

# in urls
url(r'^talent/filter\:(?P<position>[A-Za-z]+)/sort\:(?P<sort>[A-Za-z]+)$', 'talent_filter', name='talent_filter_sort'),
url(r'^talent/filter\:(?P<position>[A-Za-z]+)/$', 'talent_filter', name='talent_filter'),
url(r'^talent/sort\:(?P<sort>[A-Za-z]+)/$', 'talent_sort', name='talent_sort'),
url(r'^talent/$', 'talent', name='talent'),

# in template
<ul>
    <li>SORT BY:</li>
    <li><a href = "{% url talent_sort sort='alphabetical'%}">Alphabetical</a></li>
    ...
</ul>
<ul>
    <li><a href = '{% url talent_filter position=position%}'>{{ position }}</a></li>
    ... 
</ul>

Currently, if I am on the (unsorted, unfiltered) talent page, and I select a filter on the results, it will return talent/filter:filter. Then, if I choose to sort the results, it (obviously) goes to talent/sort:sort, removing the previous filter.

What I want to accomplish is that if I am currently on talent/filter:filter and click a sort method, it will go to talent/filter:filter/sort:sort, and if I have already sorted the results (talent/sort:sort) and click on filter, it will also take me to talent/filter:filter/sort:sort. How would I accomplish this. Thank you.

解决方案

I think one way you might be able to accomplish this is store a flag in your session that indicates what the sorting or filtering should be. For example, something like below could be used to save the state of the your sort choice.

request.session['TALANT_SORT'] = "alphabetical"
request.session['TALENT_FILTER'] = "top_ten" 

And then your views can check for the existence the session keys and apply a filter accordingly.

qs = models.MyModel.objects.all()

sort = request.session.get('TALENT_SORT', None)
if sort in ["alphabetical", "created_date"]:
    qs = qs.order_by(sort)

myfilter = request.session.get("TALENT_FILTER", None)
if myfilter in ["top_ten","bottom_ten"]:
    qs = qs.filter( .... ) 

....

The sorting and filtering could then persist across requests.

If you want to remove the sort or filter, then perhaps you can delete the session keys in the view depending on some user action:

try:
    del request.session['TALENT_SORT']
    del request.session['TALENT_FILTER']
except KeyError: 
    pass 

Also, depending on your requirements, you might consider combining the 2 urls into 1 and just use GET parameters to activate the sort.

request.GET.get('sort',None)
....
request.GET.get('filter', None)
....

These examples could probably use some more rigor, but that's the idea. Hope this helps. Joe

这篇关于使用排序&amp;在QuerySet上进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆