A DateRange Filter for Django Admin Changelists
Project description
Django DateRange Filterspec
Adds a form with AdminDateWidgets that can be used to select a range of dates for filtering the list of records displayed in an admin Changelist.
The From date
selects records with a date equal or greater than the
specified date. The To date
selects record up to, but not including
the specified date. Either field is optional. If you only enter a date
in the From date
field then all records from that date onwards will be
displayed. Similarly if you only enter a date in the To date
field then
only records before that date will be displayed.
Quick start
Install the package from PyPI:
pip install django-daterange-filterspec
The package includes a template, which loads Django's calendar widget and date shortcuts. Add the package to the INSTALLED_APPS setting so the template loader can find it:
INSTALLED_APPS = (
...
"daterange.apps.DateRangeFilterConfig",
...
)
In your ModelAdmin, set the template used for the changelist to the one
provided by the package. Then, for each field you want to filter on
create a tuple with the name of the field and the DateRangeFilter
filter class:
from django.contrib import admin
from daterange.filters import DateRangeFilter
from .models import Article
@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
list_display = ["title", "slug", "published"]
list_filter = [("published", DateRangeFilter)]
ordering = ["-created"]
change_list_template = "admin/daterange/change_list.html"
If you're already using a customised changelist template, you can add the necessary css and javascript files to the Media class for the ModelAdmin:
@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
...
class Media:
css = {"all": ("admin/css/forms.css", "css/admin/daterange.css")}
js = ("admin/js/calendar.js", "js/admin/DateRangeShortcuts.js")
Now, go forth and filter!
Demo
If you check out the project from the repository there is a fully functioning Django site that you can use to see the filter in action.
git clone git@github.com:StuartMacKay/django-daterange-filterspec.git
cd django-daterange-filterspec
Create the virtual environment:
python -m venv venv
source venv/bin/activate
pip install --upgrade pip setuptools wheel
pip install pip-tools
pip-sync requirements/dev.txt
Run the demo:
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver
Now log into the Django Admin. In the Demo section, add some Articles for different dates and filter away.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-daterange-filterspec-2.0.5.tar.gz
.
File metadata
- Download URL: django-daterange-filterspec-2.0.5.tar.gz
- Upload date:
- Size: 12.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f119db676150a8eb03efdcc377e0f48b0a5e92e9786b64b4077a59afd7886bc8 |
|
MD5 | 93febb29555fd5a4bc73d03a5f402795 |
|
BLAKE2b-256 | f857c8e5c33418a352eeacef172b2f6da70233642c52a64590a86e86f0f768b5 |
File details
Details for the file django_daterange_filterspec-2.0.5-py2.py3-none-any.whl
.
File metadata
- Download URL: django_daterange_filterspec-2.0.5-py2.py3-none-any.whl
- Upload date:
- Size: 12.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c74fb9583fbe87355cbc14e27d065b1bd8a27e85b60900909d4438ccc0baf8ec |
|
MD5 | fe89c7354232123bc0ff3ca358c5fac8 |
|
BLAKE2b-256 | b221f3f874924abe5edad724ccde864d69003c3eb2ca2111dab390f4451605f2 |