REST Condition
raw JSON → 1.0.3 verified Fri May 01 auth: no python
Rest Condition provides a simple, declarative way to compose complex permission checks for Django REST Framework views using logical operators (&, |, ~). Current version is 1.0.3, with infrequent updates.
pip install rest-condition Common errors
error 'list' object has no attribute 'has_permission' ↓
cause Using a list of permission classes instead of a single condition instance.
fix
Use rest_condition classes:
permission_classes = [And(IsAuthenticated, IsAdmin)] instead of [IsAuthenticated, IsAdmin] error TypeError: __init__() takes 1 positional argument but 2 were given ↓
cause Calling a rest_condition class without parentheses, e.g., `And(IsAuthenticated, IsAdmin)` vs using `And` alone.
fix
Ensure you instantiate the condition:
And(IsAuthenticated, IsAdmin) error AttributeError: 'And' object has no attribute 'has_permission' ↓
cause Using an uninstantiated class reference in permission_classes, e.g., `permission_classes = [And]`.
fix
Instantiate:
permission_classes = [And(IsAuthenticated)] Warnings
gotcha rest_condition classes are not callable directly; they must be instantiated. For example, use `And(IsAuthenticated)` not `And`. ↓
fix Instantiate the condition: `permission_classes = [And(IsAuthenticated, IsAdmin)]`
gotcha Order of conditions matters: `And(cond1, cond2)` short-circuits on first False, `Or` short-circuits on first True. Unexpected results can occur if side effects exist. ↓
fix Ensure no side effects in permission checks, or be aware of evaluation order.
gotcha Permission classes used inside rest_condition must have `has_permission` or `has_object_permission` methods returning bool. Mixing with DRF's `SAFE_METHODS` pattern may cause confusion. ↓
fix Define custom permissions with explicit boolean returns.
Imports
- rest_condition
from rest_condition import C, And, Or, Not - rest_condition wrong
from rest_condition import And, Or, Not, Ccorrectfrom rest_condition import And, Or, Not
Quickstart
from rest_framework.permissions import IsAuthenticated
from rest_condition import And, Or, Not
from rest_framework.views import APIView
class MyView(APIView):
permission_classes = [And(IsAuthenticated, ~Or(IsAdmin, IsOwner))]
def get(self, request):
return Response({"message": "Hello"})