2012-10-12 21:18:54 +02:00
|
|
|
Django Filter
|
|
|
|
=============
|
2012-10-12 11:08:42 +02:00
|
|
|
|
|
|
|
Django-filter is a reusable Django application for allowing users to filter
|
|
|
|
querysets dynamically.
|
|
|
|
|
2012-10-12 21:18:54 +02:00
|
|
|
Full documentation on `read the docs`_.
|
2012-10-12 11:08:42 +02:00
|
|
|
|
2016-02-28 02:21:14 +01:00
|
|
|
.. image:: https://travis-ci.org/carltongibson/django-filter.svg?branch=master
|
|
|
|
:target: https://travis-ci.org/carltongibson/django-filter
|
2012-10-16 18:47:39 +02:00
|
|
|
|
2012-10-12 21:18:54 +02:00
|
|
|
Requirements
|
|
|
|
------------
|
2012-10-12 11:08:42 +02:00
|
|
|
|
2016-11-08 17:14:24 +01:00
|
|
|
* **Python**: 2.7, 3.3, 3.4, 3.5
|
|
|
|
* **Django**: 1.8, 1.9, 1.10
|
|
|
|
* **DRF**: 3.4, 3.5
|
2012-10-12 11:08:42 +02:00
|
|
|
|
2012-10-12 21:18:54 +02:00
|
|
|
Installation
|
|
|
|
------------
|
2012-10-12 11:08:42 +02:00
|
|
|
|
2016-11-28 15:46:51 +01:00
|
|
|
Install using pip:
|
|
|
|
|
|
|
|
.. code-block:: sh
|
2012-10-12 11:08:42 +02:00
|
|
|
|
2013-03-12 20:31:28 +01:00
|
|
|
pip install django-filter
|
2012-10-12 11:08:42 +02:00
|
|
|
|
2016-11-28 15:46:51 +01:00
|
|
|
Or clone the repo and add to your ``PYTHONPATH``:
|
|
|
|
|
|
|
|
.. code-block:: sh
|
2012-10-12 11:08:42 +02:00
|
|
|
|
2016-03-11 20:38:25 +01:00
|
|
|
git clone git@github.com:carltongibson/django-filter.git
|
2012-10-12 11:08:42 +02:00
|
|
|
|
2012-10-12 21:18:54 +02:00
|
|
|
Usage
|
|
|
|
-----
|
2012-10-12 11:08:42 +02:00
|
|
|
|
|
|
|
Django-filter can be used for generating interfaces similar to the Django
|
|
|
|
admin's ``list_filter`` interface. It has an API very similar to Django's
|
|
|
|
``ModelForms``. For example, if you had a Product model you could have a
|
2016-11-26 11:18:18 +01:00
|
|
|
filterset for it with the code:
|
|
|
|
|
2016-11-28 15:46:51 +01:00
|
|
|
.. code-block:: python
|
2012-10-12 11:08:42 +02:00
|
|
|
|
|
|
|
import django_filters
|
|
|
|
|
|
|
|
class ProductFilter(django_filters.FilterSet):
|
|
|
|
class Meta:
|
|
|
|
model = Product
|
|
|
|
fields = ['name', 'price', 'manufacturer']
|
|
|
|
|
|
|
|
|
2016-11-26 11:18:18 +01:00
|
|
|
And then in your view you could do:
|
|
|
|
|
2016-11-28 15:46:51 +01:00
|
|
|
.. code-block:: python
|
2012-10-12 11:08:42 +02:00
|
|
|
|
|
|
|
def product_list(request):
|
|
|
|
filter = ProductFilter(request.GET, queryset=Product.objects.all())
|
2016-02-17 16:33:20 +01:00
|
|
|
return render(request, 'my_app/template.html', {'filter': filter})
|
2012-10-12 11:08:42 +02:00
|
|
|
|
2016-11-28 15:46:51 +01:00
|
|
|
Django-filters additionally supports specifying ``FilterSet`` fields using
|
|
|
|
a dictionary to specify filters with lookup types:
|
2016-11-26 11:18:18 +01:00
|
|
|
|
2016-11-28 15:46:51 +01:00
|
|
|
.. code-block:: python
|
2015-10-05 10:55:54 +02:00
|
|
|
|
2014-09-12 23:33:31 +02:00
|
|
|
import django_filters
|
|
|
|
|
|
|
|
class ProductFilter(django_filters.FilterSet):
|
|
|
|
class Meta:
|
|
|
|
model = Product
|
|
|
|
fields = {'name': ['exact', 'icontains'],
|
|
|
|
'price': ['exact', 'gte', 'lte'],
|
|
|
|
}
|
|
|
|
|
2016-11-28 15:46:51 +01:00
|
|
|
The filters will be available as ``'name'``, ``'name__icontains'``,
|
|
|
|
``'price'``, ``'price__gte'``, and ``'price__lte'`` in the above example.
|
2014-09-12 23:33:31 +02:00
|
|
|
|
2012-10-12 21:18:54 +02:00
|
|
|
Support
|
|
|
|
-------
|
2012-10-12 11:08:42 +02:00
|
|
|
|
|
|
|
If you have questions about usage or development you can join the
|
2012-10-12 21:18:54 +02:00
|
|
|
`mailing list`_.
|
2012-10-12 11:08:42 +02:00
|
|
|
|
2016-11-18 09:23:40 +01:00
|
|
|
.. _`read the docs`: https://django-filter.readthedocs.io/en/develop/
|
2012-10-12 21:18:54 +02:00
|
|
|
.. _`mailing list`: http://groups.google.com/group/django-filter
|