drf-rules
drf-rules is a Django REST Framework extension built on top of django-rules that provides object-level permissions fully aligned with DRF actions.
It allows you to declaratively define which users or groups can perform each action (create, list, retrieve, update, destroy, etc.) on your models and API endpoints.
Features
Simplicity (KISS): minimal setup, easy to understand.
Native DRF integration: rules map directly to DRF actions.
Consistent conventions: follows DRF’s CRUD action names (retrieve instead of view, destroy instead of delete).
Well tested and documented: high test coverage and clear examples.
Powered by django-rules: inherits its flexibility and extensibility.
Table of Contents
Requirements
Python 3.8+
Django 4.2+
Note: drf-rules supports all currently maintained Django versions and drops end-of-life versions in minor releases. See the Django Project documentation for timelines.
Installation
Using pip:
$ pip install drf-rules
Using uv:
$ uv add drf-rules
Run tests with:
$ ./runtests.sh
Django Setup
Add rules to INSTALLED_APPS:
INSTALLED_APPS = [
# ...
"rules",
]
Configure authentication backends:
AUTHENTICATION_BACKENDS = [
"rules.permissions.ObjectPermissionBackend",
"django.contrib.auth.backends.ModelBackend",
]
Defining Rules
Example with a Book model:
import rules
@rules.predicate
def is_librarian(user):
return user.groups.filter(name="librarians").exists()
@rules.predicate
def is_author(user):
return user.groups.filter(name="authors").exists()
Using with DRF
Model Permissions
Define object-level rules in Meta.rules_permissions:
import rules
from rules.contrib.models import RulesModel
class Book(RulesModel):
title = models.CharField(max_length=100)
author = models.CharField(max_length=100)
class Meta:
rules_permissions = {
"create": rules.is_staff,
"retrieve": rules.is_authenticated,
}
CRUD conventions differ slightly:
Action |
django-rules |
drf-rules |
|---|---|---|
Create |
add |
create |
Retrieve |
view |
retrieve |
Update |
change |
update / partial_update |
Delete |
delete |
destroy |
List |
view |
list |
View Permissions
Use AutoRulesPermission with your DRF views:
from rest_framework.viewsets import ModelViewSet
from drf_rules.permissions import AutoRulesPermission
class BookViewSet(ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer
permission_classes = [AutoRulesPermission]
You can also define rules for custom actions:
class Book(RulesModel):
title = models.CharField(max_length=100)
author = models.CharField(max_length=100)
class Meta:
rules_permissions = {
"create": rules.is_staff,
"retrieve": rules.is_authenticated,
"custom_nodetail": rules.is_authenticated,
":default:": rules.is_authenticated,
}
The :default: rule applies to all conventional actions (list, retrieve, create, update, partial_update, destroy) not explicitly defined.
Non-standard actions (e.g. custom_nodetail) must be defined explicitly.
Custom User Integration
If you are using a custom User model or any other custom model, you can integrate drf-rules by combining RulesModelMixin with the RulesModelBase metaclass. This ensures that permissions are automatically registered on the model.
from django.contrib.auth.models import AbstractUser
from rules.contrib.models import RulesModelMixin, RulesModelBase
class CustomUser(AbstractUser, RulesModelMixin, metaclass=RulesModelBase):
"""
Example custom user integrated with drf-rules.
You can define CRUD permissions here via Meta.rules_permissions.
"""
class Meta:
rules_permissions = {
"create": rules.is_staff,
"retrieve": rules.is_authenticated,
":default:": rules.is_authenticated,
}
If you already use a custom metaclass for your user model (or any other model), make sure it inherits from ``RulesModelBase`` so that drf-rules can register permissions correctly.
License
drf-rules is distributed under the terms of the BSD-3-Clause license.
Metadata
Release files for drf-rules 0.0.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| drf_rules-0.0.8.tar.gz | 72.3 kB | With provenance | Details for drf_rules-0.0.8.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| drf_rules-0.0.8-py3-none-any.whl | Python 3 | none | any | Details for drf_rules-0.0.8-py3-none-any.whl |
Total release size: 80.2 kB
Release files / drf_rules-0.0.8.tar.gz
| Download URL | drf_rules-0.0.8.tar.gz |
|---|---|
| Size | 72.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
30d5df72f25c952c46f0bbbaeb372363182dd64c47ac4e0b14b9d3f782d1456c
|
|
BLAKE2b-256 checksum How to use checksums |
953b42f26449db412f76c31351d682a912df3e68e57d8cc1cded30c3498efae2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
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 Sep 15, 2025.
Transparency logRelease files / drf_rules-0.0.8-py3-none-any.whl
| Download URL | drf_rules-0.0.8-py3-none-any.whl |
|---|---|
| Size | 7.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c6d176e887eae1f88f79c9dc0f8e960610af0715a61d3d700ba2697d86b36ce1
|
|
BLAKE2b-256 checksum How to use checksums |
27cf13bb86b3c74ad889f517c84855d52d15e3cd08fe6a6108ba768ccb52a2e5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
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 Sep 15, 2025.
Transparency logSecurity for drf-rules 0.0.8
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.
Attestation for drf_rules-0.0.8.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 Sep 15, 2025.
Transparency logA 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.