在Django管理中保留过滤器

编程入门 行业动态 更新时间:2024-10-23 11:33:30
本文介绍了在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管理中保留过滤器

本文发布于:2023-10-23 02:09:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519429.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:过滤器   Django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!