在Django管理员中按自定义日期范围进行过滤 [英] Filtering by custom date range in Django admin
问题描述
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 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 For example 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屋!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.date__gte
and date__lte
as GET arguments in the url. /admin/myapp/bar/?date__gte=2009-5-1&date__lt=2009-8-1