2009-12-29 80 views
5

首先,我看过question,但它已经过了一年多了。当然,在用户点击Admin中的保存按钮之后,Django 1.1.1中有一个很好的方法可以进行过滤器选择。如何在Django保存后保留过滤器选择管理

在包含数千条记录的表格中,过滤是必不可少的。而且,如果用户做出多个过滤器选择,则不必重复该过程。

回答

8

答案仍然相同:开箱即用,Django不支持这种行为。问题跟踪器中有两张票附有补丁:#3777#6903this comment中的中间件类不需要修改Django代码。

+0

我喜欢中间件解决方案。谢谢! – 2009-12-29 20:33:42

+0

不错的发现...希望我可以多次投票:) – 2009-12-29 21:48:59

+0

很好,为我的用户解决了一个BIG问题!该补丁已经在那里坐了6年了... – Roger 2013-04-06 17:59:22

0

另一种方法是使用这个片段http://djangosnippets.org/snippets/2531/

Class Modeladmin_perso(admin.ModelAdmin): 
def add_view(self, request, *args, **kwargs): 
    result = super(Modeladmin_perso, self).add_view(request, *args, **kwargs) 

    # Look at the referer for a query string '^.*\?.*$' 
    ref = request.META.get('HTTP_REFERER', '') 
    if ref.find('?') != -1: 
     # We've got a query string, set the session value 
     request.session['filtered'] = ref 

    if request.POST.has_key('_save'): 
     """ 
     We only kick into action if we've saved and if 
     there is a session key of 'filtered', then we 
     delete the key. 
     """ 
     try: 
      if request.session['filtered'] is not None: 
       result['Location'] = request.session['filtered'] 
       request.session['filtered'] = None 
     except: 
      pass 
    return result 
""" 
Used to redirect users back to their filtered list of locations if there were any 
""" 
def change_view(self, request, object_id, extra_context={}): 
    """ 
    save the referer of the page to return to the filtered 
    change_list after saving the page 
    """ 
    result = super(Modeladmin_perso, self).change_view(request, object_id, extra_context) 

    # Look at the referer for a query string '^.*\?.*$' 
    ref = request.META.get('HTTP_REFERER', '') 
    if ref.find('?') != -1: 
     # We've got a query string, set the session value 
     request.session['filtered'] = ref 

    if request.POST.has_key('_save'): 
     """ 
     We only kick into action if we've saved and if 
     there is a session key of 'filtered', then we 
     delete the key. 
     """ 
     try: 
      if request.session['filtered'] is not None: 
       result['Location'] = request.session['filtered'] 
       request.session['filtered'] = None 
     except: 
      pass 
    return result 

的好处是你不用砍东西。

0

此功能长期以来一直是对Django项目的请求(ticket已于5年前开放)。

幸运的是这个令人讨厌的行为是fixed在trunk中。期待它被包含在Django 1.6中。

3

此功能作为1.6版本的一部分添加到Django中,现在默认启用。它在release notes描述:现在

的ModelAdmin创建后保留在列表视图中的过滤器, 编辑或删除的对象。通过将preserve_filters属性 设置为False,可以恢复以前的 清除过滤器的行为。