Django admin:按“现在”过滤在列表视图中

发布于 2024-11-26 18:58:00 字数 631 浏览 3 评论 0原文

我有一个实现时间范围的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不忘初心 2024-12-03 18:58:00

您需要使用新的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文