开发者

Django admin: filtering by "now" in list view

开发者 https://www.devze.com 2023-03-23 21:27 出处:网络
I h开发者_如何学编程ave a Django Model that implements a time range, like this: class Period(models.Model):

I h开发者_如何学编程ave 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消