Django admin:按“现在”过滤在列表视图中
我有一个实现时间范围的 Django 模型,如下所示:
class Period(models.Model):
start_time = models.DateTimeField(_(u'start time'))
end_time = models.DateTimeField(_(u'end time'))
我也有一个简单的 ModelAdmin。我想在管理列表视图中提供一个过滤器,将这些时期分为“未来”、“进行中”和“过去”。我可以分别为 start_time 和 end_time 启用日期过滤器,并修改 Change_list.html 模板以提供正确的查询字符串,就像进行中的周期一样:
<li><a href="?start_time_lte=[now]&end_time_gt=[now]">In Progress</a>
我的问题是,有没有办法为 [now] 提供一些东西,即当 QuerySet 运行时评估服务器端?我知道您可以将可调用对象传递到 QuerySet 过滤器中,但 FilterSpecs 似乎不提供该功能。我讨厌必须将日期时间字符串填充到查询字符串中,因为我知道我的管理员会给过滤后的链接添加书签,并且会感到困惑。
I have a Django Model that implements a time range, like this:
class Period(models.Model):
start_time = models.DateTimeField(_(u'start time'))
end_time = models.DateTimeField(_(u'end time'))
I have a simple ModelAdmin for it as well. I'd like to provide a filter in the admin list view that buckets these Periods into "future", "in progress", and "past". I can enable date filters for the start_time and end_time separately and hack up the change_list.html template to provide the proper query string, like this for in progress Periods:
<li><a href="?start_time_lte=[now]&end_time_gt=[now]">In Progress</a>
My question is, is there any way to provide something for [now] that is evaluated server-side when the QuerySet is run? I know you can pass callables into a QuerySet filter, but it seems like that functionality isn't available with FilterSpecs. I hate to have to stuff a datetime string into the query string because I know my admins will bookmark the filtered links and will get confused.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用新的 list_filter 功能,因为黑客攻击等情况较少。
如果您无法使用 Django 1.2,我无法帮助您。
You'll want to use the new list_filter feature, because there's less hacking and such.
If you're stuck with Django 1.2, I can't help you.