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

发布于 2024-08-16 04:27:09 字数 226 浏览 3 评论 0原文

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

在一个有数千条记录的表中,过滤是必不可少的。如果用户做出了多个过滤器选择,则不必重复进行。

First, I did look at this question, but its over a year old. Surely now there is a good way in Django 1.1.1 to carry filter selection forward after a user clicks the save button in the Admin.

In a table with thousands of records, filtering is essential. And if a user makes several filter choices that effort shouldn't have to be repeated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

懒的傷心 2024-08-23 04:27:09

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

The answer is still the same: out of the box, Django doesn't support this behavior. There are a couple of tickets in the issue tracker with patches: #3777, #6903. The middleware class in this comment works without modifying Django code.

ぶ宁プ宁ぶ 2024-08-23 04:27:09

此功能已作为 1.6 版本的一部分添加到 Django,并且现在默认启用。 发行说明中对此进行了描述:

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

This feature has been added to Django as part of the 1.6 release and is enabled now by default. It is described in the release notes:

ModelAdmin now preserves filters on the list view after creating,
editing or deleting an object. It’s possible to restore the previous
behavior of clearing filters by setting the preserve_filters attribute
to False.

瑕疵 2024-08-23 04:27:09

另一种方法是使用此代码段 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

好东西是你不需要破解任何东西。

another way is to use this snippet 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 '^.*\?.*

the good thing is you don't have to hack anything.

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 '^.*\?.*

the good thing is you don't have to hack anything.

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

the good thing is you don't have to hack anything.

魂归处 2024-08-23 04:27:09

这个功能已经向 Django 项目提出了很长时间的请求(ticket 开了 5 年前)。

幸运的是,这种恼人的行为已在主干中修复。预计它会包含在 Django 1.6 中。

This feature has been a request to the Django project for a long time (the ticket was opened 5 years ago).

Fortunately this annoying behavior was fixed in trunk. Expect it to be included in Django 1.6.

箹锭⒈辈孓 2024-08-23 04:27:09

以下是我在 render_change_form 中所做的操作,以使用 preserved_filters 生成后退按钮。

def generate_back_url(self, request):
    opts = self.model._meta
    post_url = reverse(
        "admin:%s_%s_changelist" % (opts.app_label, opts.model_name),
        current_app=self.admin_site.name,
    )
    preserved_filters = self.get_preserved_filters(request)
    return add_preserved_filters(
        {"preserved_filters": preserved_filters, "opts": opts}, post_url
    )

Here's what I did inside render_change_form to generate a back button with preserved_filters.

def generate_back_url(self, request):
    opts = self.model._meta
    post_url = reverse(
        "admin:%s_%s_changelist" % (opts.app_label, opts.model_name),
        current_app=self.admin_site.name,
    )
    preserved_filters = self.get_preserved_filters(request)
    return add_preserved_filters(
        {"preserved_filters": preserved_filters, "opts": opts}, post_url
    )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文