如何在Django Admin中保存后保留过滤器选择

时间:2009-12-29 20:09:21

标签: django django-admin

首先,我确实看过这个question,但它已经超过一年了。当然,现在Django 1.1.1有一个很好的方法可以在用户点击管理员中的保存按钮后继续进行过滤器选择。

在包含数千条记录的表格中,过滤是必不可少的。如果用户选择了多个过滤器,则不必重复这些过滤。

4 个答案:

答案 0 :(得分:8)

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

答案 1 :(得分:3)

此功能已作为1.6版本的一部分添加到Django,默认情况下现已启用。它在release notes

中描述
  

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

答案 2 :(得分: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

好消息是你不必乱砍任何东西。

答案 3 :(得分:0)

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

幸运的是,这个烦人的行为在行李箱中是fixed。期待它包含在Django 1.6中。