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

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

问题描述

Django 管理站点能否按自定义日期范围选择条目,即使用两个 DateFieldsAdminDateWidget?我知道有 date_hierarchylist_filter 属性,但是当有很多数据库条目并且您只需要按精确过滤项目时,它们似乎不是很有用 date__gtedate__lte 查询.

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.

推荐答案

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

据我所知,尚未提供此功能,但您可以自己构建.

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

首先,您可以使用 date__gtedate__lte 作为 url 中的 GET 参数来过滤项目.

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

例如

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

将显示日期为 2009 年 5 月、6 月或 7 月的所有条形对象.

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

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

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.

帽子提示 丹尼尔对另一个 SO 问题的回答,它教会了我如何使用查询集过滤器参数作为 GET 参数.

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

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

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