Skip to content

Commit

Permalink
[docs] Added django-filter docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Aryamanz29 committed Mar 25, 2023
1 parent 7c64a08 commit 55d0afb
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,43 @@ which is inheriting any of the mixin classes.
Usage example: `organization_field
<https://github.com/openwisp/openwisp-users#organization_field>`_.

Django Filters Multi-tenant capabilities for the browsable web UI
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Integration of `Django Filter <https://django-filter.readthedocs.io/en/stable/guide/rest_framework.html>`_
with `Django REST Framework <https://www.django-rest-framework.org/>`_
is provided through a DRF-specific ``FilterSet`` and a ``filter backend``.

The relationship fields in ``django-filters`` options interface shows all the
relationships, without filtering by the organization the user has access to, which
breaks multi-tenancy.

The ``FilterDjangoByOrgMembership``, ``FilterDjangoByOrgManaged``
and ``FilterDjangoByOrgOwned`` can be used to solve this issue.

Usage example:

.. code-block:: python
from openwisp_users.api.mixins import FilterDjangoByOrgManaged
from ..models import FloorPlan
class FloorPlanOrganizationFilter(FilterDjangoByOrgManaged):
organization_slug = filters.CharFilter(field_name='organization__slug')
class Meta:
model = FloorPlan
fields = ['organization', 'organization_slug']
class FloorPlanListCreateView(ProtectedAPIMixin, generics.ListCreateAPIView):
serializer_class = FloorPlanSerializer
queryset = FloorPlan.objects.select_related().order_by('-created')
pagination_class = ListViewPagination
filter_backends = [filters.DjangoFilterBackend]
filterset_class = FloorPlanOrganizationFilter
Admin Multitenancy mixins
-------------------------

Expand Down

0 comments on commit 55d0afb

Please sign in to comment.