Skip to main content

django-user-permissions

User-specific permissions with per-object support for Django + Django REST Framework.

A lightweight, API-friendly object permission system that extends Django’s default permission model with:

  • Object-level permissions – grant a permission for a specific instance
  • Ownership-based access – automatically grant view, change, delete to owners
  • DRF integration – plug-and-play support for ViewSets/APIs
  • Public/safe endpoints – make certain actions public
  • Permission caching – request-local + shared cache with automatic invalidation
  • Permission syncing – create missing default permissions automatically

Think of it as a simpler alternative to django-guardian for teams building API-first applications with Django REST Framework.


What this solves vs django-guardian

While django-guardian is a massive, powerful package that supports both User and Group object-level permissions, it is often overly complex for simple API setups.

This package is a lightweight, DRF-focused alternative that solves:

  • Granular User Permissions: Easily grant specific users access to specific objects (e.g. sharing a document with one person).
  • Automatic Ownership Rules: Owners automatically get permissions to their objects without needing to create explicit database records for every single object created.
  • DRF Action Mapping: It maps API actions (list, retrieve, etc.) directly to standard Django permissions (view, change, etc.) right out of the box.
  • Auto-Filtering Querysets: Simply setting auto_filter_queryset = True automatically hides objects the user doesn't own or have explicit permission to view.

(Note: If you need to grant an entire Group access to a specific object, django-guardian is recommended, as this package strictly focuses on user-to-object granular permissions out of the box. Global group permissions still work perfectly!)


Requirements

  • Python 3.8+
  • Django 3.2+
  • Django REST Framework 3.14+

Installation

pip install django-user-permissions

Setup

Add to installed apps

INSTALLED_APPS = [
    ...
    "django_user_permissions",
]

Add authentication backend

Place this before Django’s default backend:

AUTHENTICATION_BACKENDS = [
    "django_user_permissions.backends.UserSpecificPermissionBackend",
    "django.contrib.auth.backends.ModelBackend",
]

Run migrations

python manage.py migrate

Sync permissions

Create missing permissions:

python manage.py sync_user_permissions

Dry run:

python manage.py sync_user_permissions --dry-run

Configuration

Add this in settings.py:

USER_PERMISSIONS = {
    "OWNERSHIP_FIELD": "user",
    "OWNER_AUTO_PERMISSIONS": ["view", "change", "delete"],
    "ALLOW_CREATE_ON_OWNERSHIP": True,

    # Actions that can be resolved at object level
    "ALLOW_OBJECT_LEVEL_ACTIONS": [
        "retrieve",
        "update",
        "partial_update",
        "destroy"
    ],

    # Allow list endpoints even if final queryset is empty
    "LIST_ALLOW_EMPTY": True,

    "MODEL_SAFE_METHODS": {
        "document": ["list", "retrieve"]
    },

    "CACHE_TTL": 300
}

DRF Integration

Example model

class Document(models.Model):
    title = models.CharField(max_length=255)
    user = models.ForeignKey(User, on_delete=models.CASCADE)

Example ViewSet

from django_user_permissions.mixins import UserSpecificPermissionMixin

class DocumentViewSet(UserSpecificPermissionMixin, ModelViewSet):
    queryset = Document.objects.all()
    serializer_class = DocumentSerializer
    auto_filter_queryset = True

auto_filter_queryset=True filters the queryset to objects the user can view (global or per-object permissions) plus objects they own—even without explicit permissions.


DRF Action Mapping

DRF Action Permission Required Example
list view_<model_name> view_document
retrieve view_<model_name> view_document
create add_<model_name> add_document
update change_<model_name> change_document
partial_update change_<model_name> change_document
destroy delete_<model_name> delete_document

Example:

PATCH /documents/1/

Requires:

change_document

How Permission Checks Work

flowchart LR
    A[Request] --> B{Authenticated?}
    B -->|No| DENY([Deny])
    B -->|Yes| C{Superuser?}
    C -->|Yes| ALLOW([Allow])
    C -->|No| D{Safe/public?}
    D -->|Yes| ALLOW
    D -->|No| E{Global perm?}
    E -->|Yes| ALLOW
    E -->|No| F{Create + ownership?}
    F -->|Yes| ALLOW
    F -->|No| G{Per‑object perm?}
    G -->|Yes| ALLOW
    G -->|No| H{Owner & allowed?}
    H -->|Yes| ALLOW
    H -->|No| DENY

Permission evaluation order:

  1. Superuser access
  2. Safe/public method access
  3. Global Django permissions
  4. Ownership create validation
  5. Object-specific permissions
  6. Ownership auto-permissions

Grant Object-Level Permission

from django_user_permissions.models import UserPermission
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType

doc = Document.objects.get(pk=1)
perm = Permission.objects.get(codename="change_document")

UserPermission.objects.create(
    user=user,
    permission=perm,
    content_type=ContentType.objects.get_for_model(doc),
    object_id=doc.id
)

Now that user can modify only that specific document.


Caching

This package uses:

  • request-level caching
  • shared caching
  • automatic signal-based invalidation

Whenever permissions are updated/deleted, stale cache gets cleared automatically.


Management Commands

python manage.py sync_user_permissions

Supports:

  • --dry-run
  • --apps
  • --exclude

Testing

pip install -e .[dev]
pytest tests/

Use Cases

  • SaaS platforms: Users own their own data but occasionally need to grant granular access to a specific support agent or manager.
  • Document sharing apps: A user uploads a private document and explicitly shares "view" access with exactly one other specific user.
  • Multi-tenant products: You need strict data isolation per user out of the box for DRF endpoints.
  • Internal admin tools: Specific employees need granular "edit" permissions on specific high-value database rows.

Roadmap

  • Group object permissions
  • Queryset filtering helpers
  • Admin dashboard improvements
  • Async cache invalidation

License

MIT License


Built because writing repetitive permission logic in every DRF project gets exhausting.

Metadata

Release files for django-user-permissions 0.2.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 django-user-permissions 0.2.0
File Size Uploaded Provenance Actions
django_user_permissions-0.2.0.tar.gz 18.5 kB Without provenance Details for django_user_permissions-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-user-permissions 0.2.0
File Interpreter ABI Platform Actions
django_user_permissions-0.2.0-py3-none-any.whl Python 3 none any Details for django_user_permissions-0.2.0-py3-none-any.whl

Total release size: 36.7 kB

Release files / django_user_permissions-0.2.0.tar.gz

Download URL django_user_permissions-0.2.0.tar.gz
Size 18.5 kB
Tags Source
SHA-256 checksum
How to use checksums
079539551f7ae7d67daa26d07778aaab779a459915f0ca84159de4b58621c2ec
BLAKE2b-256 checksum
How to use checksums
ad09396e7e3e0ee7e63c1b2c894abc5f99415d059e494e7ff424d168880c4533
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.13

Release files / django_user_permissions-0.2.0-py3-none-any.whl

Download URL django_user_permissions-0.2.0-py3-none-any.whl
Size 18.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ab349b7ec611fec1d40dd7868a261296bdf3218025cc4430376ae40f26731f8b
BLAKE2b-256 checksum
How to use checksums
12b773829247cdb4aa8f4999499c26ada32489063ddc0534d15c3eab61ee8c6d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.13

Security for django-user-permissions 0.2.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.

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.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No 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