# 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.
![Filter list](https://raw.githubusercontent.com/StuartMacKay/django-daterange-filterspec/master/docs/images/screenshot.png)
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:
```shell
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:
```python
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:
```python
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:
```python
@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.
```shell
git clone git@github.com:StuartMacKay/django-daterange-filterspec.git
cd django-daterange-filterspec
```
Create the virtual environment:
```shell
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:
```shell
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.
Raw data
{
"_id": null,
"home_page": "",
"name": "django-daterange-filterspec",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "Django Admin DateRage Filter Changelists",
"author": "Stuart MacKay",
"author_email": "smackay@flagstonesoftware.com",
"download_url": "https://files.pythonhosted.org/packages/f8/57/c8e5c33418a352eeacef172b2f6da70233642c52a64590a86e86f0f768b5/django-daterange-filterspec-2.0.5.tar.gz",
"platform": null,
"description": "# Django DateRange Filterspec\n\nAdds a form with AdminDateWidgets that can be used to select a range of\ndates for filtering the list of records displayed in an admin Changelist.\n\n![Filter list](https://raw.githubusercontent.com/StuartMacKay/django-daterange-filterspec/master/docs/images/screenshot.png)\n\nThe `From date` selects records with a date equal or greater than the \nspecified date. The `To date` selects record up to, but not including\nthe specified date. Either field is optional. If you only enter a date \nin the `From date` field then all records from that date onwards will be\ndisplayed. Similarly if you only enter a date in the `To date` field then\nonly records before that date will be displayed.\n\n## Quick start\n\nInstall the package from PyPI:\n\n```shell\npip install django-daterange-filterspec\n```\n\nThe package includes a template, which loads Django's calendar widget\nand date shortcuts. Add the package to the INSTALLED_APPS setting so the\ntemplate loader can find it:\n\n```python\nINSTALLED_APPS = (\n ...\n \"daterange.apps.DateRangeFilterConfig\",\n ...\n)\n```\n\nIn your ModelAdmin, set the template used for the changelist to the one\nprovided by the package. Then, for each field you want to filter on\ncreate a tuple with the name of the field and the `DateRangeFilter`\nfilter class:\n\n```python\nfrom django.contrib import admin\n\nfrom daterange.filters import DateRangeFilter\n\nfrom .models import Article\n\n\n@admin.register(Article)\nclass ArticleAdmin(admin.ModelAdmin):\n\n list_display = [\"title\", \"slug\", \"published\"]\n list_filter = [(\"published\", DateRangeFilter)]\n ordering = [\"-created\"]\n\n change_list_template = \"admin/daterange/change_list.html\"\n```\n\nIf you're already using a customised changelist template, you can add the necessary\ncss and javascript files to the Media class for the ModelAdmin:\n\n```python\n@admin.register(Article)\nclass ArticleAdmin(admin.ModelAdmin):\n\n ...\n\n class Media:\n css = {\"all\": (\"admin/css/forms.css\", \"css/admin/daterange.css\")}\n js = (\"admin/js/calendar.js\", \"js/admin/DateRangeShortcuts.js\")\n```\n\nNow, go forth and filter!\n\n## Demo\n\nIf you check out the project from the repository there is a fully functioning\nDjango site that you can use to see the filter in action.\n\n```shell\ngit clone git@github.com:StuartMacKay/django-daterange-filterspec.git\ncd django-daterange-filterspec\n```\n\nCreate the virtual environment:\n\n```shell\npython -m venv venv\nsource venv/bin/activate\n\npip install --upgrade pip setuptools wheel\npip install pip-tools\npip-sync requirements/dev.txt\n```\n\nRun the demo:\n\n```shell\npython manage.py migrate\npython manage.py createsuperuser\npython manage.py runserver\n```\n\nNow log into the Django Admin. In the Demo section, add some Articles\nfor different dates and filter away.\n",
"bugtrack_url": null,
"license": "License :: OSI Approved :: BSD License",
"summary": "A DateRange Filter for Django Admin Changelists",
"version": "2.0.5",
"project_urls": null,
"split_keywords": [
"django",
"admin",
"daterage",
"filter",
"changelists"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "b221f3f874924abe5edad724ccde864d69003c3eb2ca2111dab390f4451605f2",
"md5": "fe89c7354232123bc0ff3ca358c5fac8",
"sha256": "c74fb9583fbe87355cbc14e27d065b1bd8a27e85b60900909d4438ccc0baf8ec"
},
"downloads": -1,
"filename": "django_daterange_filterspec-2.0.5-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "fe89c7354232123bc0ff3ca358c5fac8",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": ">=3.7",
"size": 12608,
"upload_time": "2023-10-23T10:15:18",
"upload_time_iso_8601": "2023-10-23T10:15:18.290322Z",
"url": "https://files.pythonhosted.org/packages/b2/21/f3f874924abe5edad724ccde864d69003c3eb2ca2111dab390f4451605f2/django_daterange_filterspec-2.0.5-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "f857c8e5c33418a352eeacef172b2f6da70233642c52a64590a86e86f0f768b5",
"md5": "93febb29555fd5a4bc73d03a5f402795",
"sha256": "f119db676150a8eb03efdcc377e0f48b0a5e92e9786b64b4077a59afd7886bc8"
},
"downloads": -1,
"filename": "django-daterange-filterspec-2.0.5.tar.gz",
"has_sig": false,
"md5_digest": "93febb29555fd5a4bc73d03a5f402795",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 12416,
"upload_time": "2023-10-23T10:15:19",
"upload_time_iso_8601": "2023-10-23T10:15:19.930384Z",
"url": "https://files.pythonhosted.org/packages/f8/57/c8e5c33418a352eeacef172b2f6da70233642c52a64590a86e86f0f768b5/django-daterange-filterspec-2.0.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-10-23 10:15:19",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "django-daterange-filterspec"
}