在Django管理员中按自定义日期范围进行过滤 [英] Filtering by custom date range in Django admin

查看:406
本文介绍了在Django管理员中按自定义日期范围进行过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django管理站点可以按照自定义日期范围选择条目,即使用两个 DateFields AdminDateWidget ?我知道有 date_hierarchy list_filter 属性,但是当有很多数据库条目,你只需要按照 date__gte date__lte 查询过滤项目。

解决方案

注意:我在2009年写了这个答案,当时所需的功能在Django作为公共API不可用。对于Django 1.4+,请参阅其他答案。



只要我知道这个功能是没有提供的,但是你可以自己构建它。



首先,您可以使用 date__gte date__lte 作为GET参数在url。



例如

  / admin / myapp / bar /?date__gte = 2009-5-1& date__lt = 2009-8-1 

将显示所有带有日期的条形对象在2009年5月,6月或7月。



然后,如果你覆盖 admin / change_list.html 模板文件,您可以添加开始和结束日期的小部件,导航到所需的URL。






帽子提示 Daniel的答案。 p>

Can Django admin site select entries by a custom date range, i.e. using two DateFields with AdminDateWidget? I know that there are date_hierarchy and list_filter properties, but they don't seem very useful when there are a lot of DB entries and you just need to filter items by exact date__gte and date__lte query.

Note: I wrote this answer in 2009, when the required functionality was not available in Django as a public API. For Django 1.4+, see the other answers.

This functionality isn't provided as far as I'm aware, but you can build it yourself.

Firstly, you can filter items using date__gte and date__lte as GET arguments in the url.

For example

/admin/myapp/bar/?date__gte=2009-5-1&date__lt=2009-8-1

will display all bar objects with dates in May, June or July 2009.

Then if you override the admin/change_list.html template file, you can add widgets for start and end dates, which navigate to the required url.


Hat tip to Daniel's answer to another SO question, which taught me about using queryset filter parameters as GET arguments.

这篇关于在Django管理员中按自定义日期范围进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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