Skip to main content
`django-rest-framework-chain` is an extension to Django REST Framework that allows arbitrary chaining of both relations and lookup filters.

.. image:: https://secure.travis-ci.org/philipn/django-rest-framework-chain.png?branch=master
:target: http://travis-ci.org/philipn/django-rest-framework-chain

Installation
------------

.. code-block:: bash

$ pip install djangorestframework-chain

Requirements
------------

* Python 2.6+
* Django 1.4.5+

Usage
-----

Chaining filtering through relations
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To enable chained filtering through relations:

.. code-block:: python

from rest_framework import viewsets
import django_filters
from rest_framework_chain import ChainedFilterSet, RelatedFilter

# Just a regular FilterSet
class UserFilter(django_filters.FilterSet):
username = django_filters.CharFilter(name='username')
...

class PageFilter(ChainedFilterSet):
title = django_filters.CharFilter(name='title')
author = RelatedFilter(UserFilter, name='author')

# Then just use the PageFilter as you would any other FilterSet:

class PageView(viewsets.ModelViewSet):
...
filter_class = PageFilter

then we can automatically chain our filters through the ``author`` relation, as so::

/api/page/?author__username=philipn


Allowing any lookup type on a field
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

We can use the ``AllLookupsFilter`` to allow all possible lookup types on a particular
field. While we could otherwise specify these by hand, e.g.:

.. code-block:: python

class ProductFilter(django_filters.FilterSet):
min_price = django_filters.NumberFilter(name="price", lookup_type='gte')
...

to allow the ``price__gte`` lookup. But this gets cumbersome, and we sometimes want to
allow any possible lookups on particular fields. We do this by using ``AllLookupsFilter``:

.. code-block:: python

from rest_framework import viewsets
import django_filters
from rest_framework_chain import ChainedFilterSet, AllLookupsFilter

class PageFilter(ChainedFilterSet):
title = AllLookupsFilter(name='title')
...

then we can use any possible lookup on the ``title`` field, e.g.::

/api/page/?title__icontains=park

or ::

/api/page/?title__startswith=The

and also filter on the default lookup (``exact``), as usual::

/api/page/?title=The%20Park

Combining RelatedFilter and AllLookupsFilter
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

We can combine ``RelatedFilter`` and ``AllLookupsFilter``:

.. code-block:: python

from rest_framework import viewsets
import django_filters
from rest_framework_chain import ChainedFilterSet, RelatedFilter

class PageFilter(ChainedFilterSet):
title = django_filters.CharFilter(name='title')
author = RelatedFilter(UserFilter, name='author')

# Just a regular FilterSet
class UserFilter(ChainedFilterSet):
username = AllLookupsFilter(name='username')
...

then we can filter like so::

/api/page/?author__username__icontains=john

More information on RelatedFilter
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Recursive relations are supported. You will need to specify the full module
path in the ``RelatedFilter`` definition in some cases, e.g.:

.. code-block:: python

class PersonFilter(ChainedFilterSet):
name = AllLookupsFilter(name='name')
best_friend = RelatedFilter('people.views.PersonFilter', name='best_friend')

class Meta:
model = Person

License
-------
Copyright (c) 2013 Philip Neustrom <philipn@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Metadata

Release files for djangorestframework-chain 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for djangorestframework-chain 0.1.3
File Size Uploaded Provenance
djangorestframework-chain-0.1.3.tar.gz 9.0 kB Without provenance Details

Release files / djangorestframework-chain-0.1.3.tar.gz

Download URL djangorestframework-chain-0.1.3.tar.gz
Size 9.0 kB
Tags Source
SHA-256 checksum
How to use checksums
8940e20766f070ec5d8f920c528e1fbf910ff170bc8a0cbfb80237ad4475f954
BLAKE2b-256 checksum
How to use checksums
2de83fec50f70103d6c772cebb9f36731c39d6f5b48de7982d5343528b4ff997
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for djangorestframework-chain 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page