Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering by custom date range in Django admin

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.

like image 751
Dmitry Gladkov Avatar asked Nov 03 '09 15:11

Dmitry Gladkov


1 Answers

In django 1.4, you can user list_filter. try:

from django.contrib.admin import DateFieldListFilter class PersonAdmin(ModelAdmin):     list_filter = (         ('date_field_name', DateFieldListFilter),     ) 

This will give some built-in ranges, but it will work if you put the date range in url, like:

?date__gte=2009-5-1&date__lt=2009-8-1 

If you need a date picker (like jquery), then you need to extend DateFieldListFilter. I sent a patch to django-admin-filtrate, so, check there soon.

like image 145
bsm Avatar answered Sep 27 '22 15:09

bsm