Skip to main content

drf-hierarchy

License: GPL v3 CI Release

drf-hierarchy is a reusable Django app that adds hierarchical group permissions on top of Django auth.Group using MPTT (tree structure).

It is designed for projects where permissions should be inherited through a group tree (for example: organization units, tenants, folders, or role hierarchies).

What problem this app solves

By default, Django groups are flat. This app provides:

  • A tree model (MPTTGroup) that wraps auth.Group.
  • Permission inheritance across parent/child nodes.
  • A DRF endpoint to expose/manage the hierarchy.
  • Admin integration with tree visualization.
  • Validation guards for circular references and max children constraints.

Compatibility

  • Python: >=3.10,<3.13
  • Django: >=4.2,<7.0
  • djangorestframework: >=3.15,<3.17
  • django-mptt: >=0.16,<1.0

Validated test matrix in this repository:

  • Django 4.2.x
  • Django 5.x
  • Django 6.x

CI/CD

  • CI (.github/workflows/ci.yml): lint, Django checks and tests on push/PR, plus compatibility matrix across Django 4.2/5.x/6.x.
  • CD (.github/workflows/release.yml): on GitHub Release publication, builds the package and publishes to PyPI.

Versioning (SemVer)

This project follows Semantic Versioning using X.Y.Z in pyproject.toml and tags in the form vX.Y.Z.

  • MAJOR (X): backward-incompatible API/behavior changes.
  • MINOR (Y): backward-compatible features.
  • PATCH (Z): backward-compatible fixes.

Automated checks:

  • CI validates that project.version uses SemVer core format.
  • Release workflow validates that release tag vX.Y.Z matches project.version.

Recommended release flow:

  1. Update project.version in pyproject.toml.
  2. Merge to main/master.
  3. Create tag vX.Y.Z on that commit.
  4. Publish GitHub Release for that tag.

Installation

uv add drf-hierarchy

For local development of this repository:

cp .env.example .env
uv sync --group dev
uv run python manage.py migrate
uv run python manage.py createsuperuser
uv run python manage.py runserver 0.0.0.0:8000

Alternative (pip)

pip install drf-hierarchy

Integrating into an existing Django project

1. Add installed apps

INSTALLED_APPS = [
    # ...
    "rest_framework",
    "mptt",
    "hierarchy",
]

2. Enable authentication backend

AUTHENTICATION_BACKENDS = [
    "django.contrib.auth.backends.ModelBackend",
    "hierarchy.backends.MPTTGroupBackend",
]

MPTTGroupBackend augments Django group permission resolution by traversing the hierarchy.

3. Run migrations

python manage.py migrate
from django.urls import path
from rest_framework import routers
from hierarchy.views import MPTTGroupViewSet

router = routers.DefaultRouter()
router.register("hierarchy", MPTTGroupViewSet)

urlpatterns = [
    path("admin/", admin.site.urls),
] + router.urls

Configuration

The app supports these settings:

# Traversal method used to compute inherited permissions.
# Must be one of:
# get_ancestors, get_children, get_descendants, get_family,
# get_next_sibling, get_previous_sibling, get_root, get_siblings
HIERARCHY_MPTT_METHOD = "get_descendants"

# Max serialized depth for recursive children in API responses.
HIERARCHY_MAX_SERIALIZATION_DEPTH = 5

# Optional global maximum number of children per node (project-defined usage).
HIERARCHY_MAX_CHILDREN = None

Notes:

  • HIERARCHY_MPTT_METHOD is validated at startup.
  • HIERARCHY_MAX_SERIALIZATION_DEPTH prevents unbounded recursive payloads.

Data model and behavior

MPTTGroup fields:

  • group (OneToOne with auth.Group)
  • parent (tree parent)
  • inheritable (whether node permissions are inheritable)
  • max_children (per-node children limit)
  • active, propagate (state fields)

Validation rules:

  • A node cannot be its own parent.
  • A node cannot set one of its descendants as parent.
  • Parent max_children is enforced.

Security and production guidance

This repository includes a sample project for demos/tests.

Before deploying any real project:

  • Set DEBUG=False.
  • Use a strong SECRET_KEY.
  • Configure ALLOWED_HOSTS.
  • Enable secure cookies and HTTPS/HSTS settings.
  • Run:
python manage.py check --deploy

The included .env.example shows secure-related variables you can set.

Development workflow (this repository)

uv sync --group dev
uv run ruff check .
uv run pytest
uv run python manage.py check --settings=sample.settings
uv run python manage.py check --deploy --settings=sample.settings

Admin usage

The app registers MPTTGroup in Django admin using DraggableMPTTAdmin, so the hierarchy can be managed visually.

API overview

Default sample API route:

  • GET /hierarchy/ root hierarchy nodes
  • standard DRF ModelViewSet actions for CRUD

Access is protected by DjangoModelPermissions.

License

GPLv3. See LICENSE.

Metadata

Release files for drf-hierarchy 0.1.0

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

Source distribution (sdist)

Source distribution for drf-hierarchy 0.1.0
File Size Uploaded Provenance Actions
drf_hierarchy-0.1.0.tar.gz 35.4 kB With provenance Details for drf_hierarchy-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for drf-hierarchy 0.1.0
File Interpreter ABI Platform Actions
drf_hierarchy-0.1.0-py3-none-any.whl Python 3 none any Details for drf_hierarchy-0.1.0-py3-none-any.whl

Total release size: 61.5 kB

Release files / drf_hierarchy-0.1.0.tar.gz

Download URL drf_hierarchy-0.1.0.tar.gz
Size 35.4 kB
Tags Source
SHA-256 checksum
How to use checksums
47efd3bb789f46300a8be90ee27be04cb704bb9d3f8da8791d34924c22671908
BLAKE2b-256 checksum
How to use checksums
68beac10ae6e65cca33fb00d9f1d62e2a95da1cae91a56a8c74e061c3341a78a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 20, 2026.

Transparency log

Release files / drf_hierarchy-0.1.0-py3-none-any.whl

Download URL drf_hierarchy-0.1.0-py3-none-any.whl
Size 26.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
90a79f78e65ccc6130429fb4cdf53767d0817ca9153ce2b39997c134fe333350
BLAKE2b-256 checksum
How to use checksums
da79eeb830c4ffc1d91d066c6dc5ad8b2afa95089dab280cf2fa2baddc0e1cad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 20, 2026.

Transparency log

Security for drf-hierarchy 0.1.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for drf_hierarchy-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 20, 2026.

Transparency log

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

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