在Django管理中保留过滤器 [英] Keeping filters in Django Admin

查看:137
本文介绍了在Django管理中保留过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的是:




  • 我去管理网站,将一些过滤器应用于对象列表

  • 我点击并对象编辑,编辑,编辑,点击保存

  • 网站带我到对象列表...未过滤。我想要将第1步的过滤器记住并应用。



有没有办法做到这一点? >

解决方案

不幸的是没有简单的方法来做到这一点。过滤似乎没有保存在任何会话变量中。



回到两次是正常的方法,但如果刚更改了一个对象,以便不再使用您的过滤器显示。



如果只是一次性,请点击两次或再次进行过滤,这是最简单的方法



如果您要更频繁地进行过滤,或者您只想了解如何窃取管理员(这是相当开放和容易的),您将需要撰写 FilterSpec



查看此处这里为自己写的人的例子。



一个真正的,非常糟糕的方式来做这件事我要编辑管理界面,以便在您点击保存后,您将被重定向到您过滤的URL。我不会推荐这个,但这是一个选项。



另一个相当简单的方法是写一个通用视图来显示已过滤的对象,然后使用Django表单来编辑这些项目。我会看看这个,你会惊讶的是你需要写多少代码来获得一个简单的视图/编辑页面。


What I would like to achive is:

  • I go to admin site, apply some filters to the list of objects
  • I click and object edit, edit, edit, hit 'Save'
  • Site takes me to the list of objects... unfiltered. I'd like to have the filter from step 1 remembered and applied.

Is there an easy way to do it?

解决方案

Unfortunately there's no easy way to do this. The filtering does not seem to be saved in any session variable.

Clicking back twice is the normal method, but it can be unweildy and annoying if you've just changed an object so that it should no longer be shown using your filter.

If it's just a one-off, click back twice or go through the filtering again, it's the easiest way.

If you're going to be filtering more often, or you just want to learn about hacking the admin (which is pretty open and easy), you'll want to write a FilterSpec.

Have a look here and here for examples of people writing their own.

A really, really terrible way to do this would be to edit the admin interface so that after you click "Save", you are redirected to you filtered URL. I wouldn't recommend this at all, but it's an option.

Another fairly simple way to do this would be to write a generic view to show your filtered objects, then use Django forms to edit the items from there. I'd have a look at this, you'll be stunned just how little code you have to write to get a simple view/edit page going.

这篇关于在Django管理中保留过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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