Skip to main content

Django Ninja Shield

A powerful and flexible permissions control package for Django Ninja. This package allows you to easily manage and combine permissions for your Django Ninja API endpoints using a clean and intuitive syntax.

Features

  • 🛡️ Simple permission decorators for Django Ninja endpoints
  • ⚡ Combine permissions using logical operations (AND, OR, NOT)
  • 🔒 Built on top of Django's permission system
  • 🚀 Type-safe with full typing support

Installation

You can install the package using pip:

pip install django-ninja-shield

Or if you're using Poetry:

poetry add django-ninja-shield

Basic Usage

Here's a simple example of how to use django-ninja-shield:

from django_ninja_shield import requires_permissions
from ninja import Router

router = Router()

@router.get("/articles")
@requires_permissions("articles.view_article")
def get_articles(request):
    return {"message": "You have permission to view articles"}

Advanced Usage

Combining Permissions

You can combine multiple permissions using logical operators:

from django_ninja_shield import requires_permissions, P

router = Router()

# Using AND operator
@router.post("/articles")
@requires_permissions(P("articles.add_article") & P("articles.change_article"))
def create_article(request):
    return {"message": "You have permission to create articles"}

# Using OR operator
@router.get("/dashboard")
@requires_permissions(P("admin.view_dashboard") | P("staff.view_dashboard"))
def view_dashboard(request):
    return {"message": "You have permission to view the dashboard"}

# Using NOT operator
@router.get("/public")
@requires_permissions(~P("articles.is_restricted"))
def public_view(request):
    return {"message": "This is a public view"}

Complex Permission Combinations

You can create complex permission rules by combining multiple operations:

from django_ninja_shield import requires_permissions, P, IsAdmin, IsUseruser, IsStaff, IsActive

@router.put("/articles/{article_id}")
@requires_permissions(
    (P("articles.change_article") & P("articles.view_article")) |
    IsAdmin() # or: IsStaff(), IsSuperuser, IsActive
)
def update_article(request, article_id: int):
    return {"message": f"Article {article_id} updated"}

Response Format

When permission is denied, the API returns a 403 Forbidden response with the following format:

{
  "detail": "Permission denied"
}

Requirements

  • Python ≥ 3.10
  • Django ≥ 4.0.0

License

This project is licensed under the MIT License.

Metadata

Release files for django-ninja-shield 0.0.8

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-ninja-shield 0.0.8
File Size Uploaded
django_ninja_shield-0.0.8.tar.gz 3.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-ninja-shield 0.0.8
File Interpreter ABI Platform
django_ninja_shield-0.0.8-py3-none-any.whl Python 3 none any Details

Total release size: 8.8 kB

Release files / django_ninja_shield-0.0.8.tar.gz

Download URL django_ninja_shield-0.0.8.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
4028d952b11e19d6bc013b4c967a7a737e8166c4fd2d4cc085e0429bdb08f8c9
BLAKE2b-256 checksum
How to use checksums
4419a373ddaa66569370296f7a9f1a8abd03a083ac655f1cc1a94525b80aa890
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.13.1 Linux/6.11.0-1018-azure

Release files / django_ninja_shield-0.0.8-py3-none-any.whl

Download URL django_ninja_shield-0.0.8-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2b59a7cdb2b3e1898489827e40babb9d59b44d678c5ea02065c1966544034b7e
BLAKE2b-256 checksum
How to use checksums
00fe1fe8123790e53e93028b56483c8d979373048be088c2d7ecdfac693161ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.13.1 Linux/6.11.0-1018-azure

Release history Release notifications | RSS feed

This release

0.0.8 This release

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

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