| 1 |
Django Filter |
| 2 |
============= |
| 3 |
|
| 4 |
Django-filter is a reusable Django application allowing users to declaratively |
| 5 |
add dynamic ``QuerySet`` filtering from URL parameters. |
| 6 |
|
| 7 |
Full documentation on `read the docs`_. |
| 8 |
|
| 9 |
.. image:: https://raw.githubusercontent.com/carltongibson/django-filter/python-coverage-comment-action-data/badge.svg |
| 10 |
:target: https://github.com/carltongibson/django-filter/tree/python-coverage-comment-action-data |
| 11 |
|
| 12 |
.. image:: https://badge.fury.io/py/django-filter.svg |
| 13 |
:target: http://badge.fury.io/py/django-filter |
| 14 |
|
| 15 |
|
| 16 |
Versioning and stability policy |
| 17 |
------------------------------- |
| 18 |
|
| 19 |
Django-Filter is a mature and stable package. It uses a two-part CalVer |
| 20 |
versioning scheme, such as ``21.1``. The first number is the year. The second |
| 21 |
is the release number within that year. |
| 22 |
|
| 23 |
On an on-going basis, Django-Filter aims to support all current Django |
| 24 |
versions, the matching current Python versions, and the latest version of |
| 25 |
Django REST Framework. |
| 26 |
|
| 27 |
Please see: |
| 28 |
|
| 29 |
* `Status of supported Python versions <https://devguide.python.org/versions/#supported-versions>`_ |
| 30 |
* `List of supported Django versions <https://www.djangoproject.com/download/#supported-versions>`_ |
| 31 |
|
| 32 |
Support for Python and Django versions will be dropped when they reach |
| 33 |
end-of-life. Support for Python versions will be dropped when they reach |
| 34 |
end-of-life, even when still supported by a current version of Django. |
| 35 |
|
| 36 |
Other breaking changes are rare. Where required, every effort will be made to |
| 37 |
apply a "Year plus two" deprecation period. For example, a change initially |
| 38 |
introduced in ``23.x`` would offer a fallback where feasible and finally be |
| 39 |
removed in ``25.1``. Where fallbacks are not feasible, breaking changes without |
| 40 |
deprecation will be called out in the release notes. |
| 41 |
|
| 42 |
|
| 43 |
Installation |
| 44 |
------------ |
| 45 |
|
| 46 |
Install using pip: |
| 47 |
|
| 48 |
.. code-block:: sh |
| 49 |
|
| 50 |
pip install django-filter |
| 51 |
|
| 52 |
Then add ``'django_filters'`` to your ``INSTALLED_APPS``. |
| 53 |
|
| 54 |
.. code-block:: python |
| 55 |
|
| 56 |
INSTALLED_APPS = [ |
| 57 |
... |
| 58 |
'django_filters', |
| 59 |
] |
| 60 |
|
| 61 |
|
| 62 |
Usage |
| 63 |
----- |
| 64 |
|
| 65 |
Django-filter can be used for generating interfaces similar to the Django |
| 66 |
admin's ``list_filter`` interface. It has an API very similar to Django's |
| 67 |
``ModelForms``. For example, if you had a Product model you could have a |
| 68 |
filterset for it with the code: |
| 69 |
|
| 70 |
.. code-block:: python |
| 71 |
|
| 72 |
import django_filters |
| 73 |
|
| 74 |
class ProductFilter(django_filters.FilterSet): |
| 75 |
class Meta: |
| 76 |
model = Product |
| 77 |
fields = ['name', 'price', 'manufacturer'] |
| 78 |
|
| 79 |
|
| 80 |
And then in your view you could do: |
| 81 |
|
| 82 |
.. code-block:: python |
| 83 |
|
| 84 |
def product_list(request): |
| 85 |
filter = ProductFilter(request.GET, queryset=Product.objects.all()) |
| 86 |
return render(request, 'my_app/template.html', {'filter': filter}) |
| 87 |
|
| 88 |
|
| 89 |
Usage with Django REST Framework |
| 90 |
-------------------------------- |
| 91 |
|
| 92 |
Django-filter provides a custom ``FilterSet`` and filter backend for use with |
| 93 |
Django REST Framework. |
| 94 |
|
| 95 |
To use this adjust your import to use |
| 96 |
``django_filters.rest_framework.FilterSet``. |
| 97 |
|
| 98 |
.. code-block:: python |
| 99 |
|
| 100 |
from django_filters import rest_framework as filters |
| 101 |
|
| 102 |
class ProductFilter(filters.FilterSet): |
| 103 |
class Meta: |
| 104 |
model = Product |
| 105 |
fields = ('category', 'in_stock') |
| 106 |
|
| 107 |
|
| 108 |
For more details see the `DRF integration docs`_. |
| 109 |
|
| 110 |
|
| 111 |
Support |
| 112 |
------- |
| 113 |
|
| 114 |
If you need help you can start a `discussion`_. For commercial support, please |
| 115 |
`contact Carlton Gibson via his website <https://noumenal.es/>`_. |
| 116 |
|
| 117 |
.. _`discussion`: https://github.com/carltongibson/django-filter/discussions |
| 118 |
.. _`read the docs`: https://django-filter.readthedocs.io/en/main/ |
| 119 |
.. _`DRF integration docs`: https://django-filter.readthedocs.io/en/stable/guide/rest_framework.html |