djcrud_drf¶
Optional DRF API layer for djcrud. Install separately — core djcrud has no DRF dependency.
Install¶
pip install --pre "djcrud[drf]"
Add to INSTALLED_APPS and include API URLs:
INSTALLED_APPS += [
"rest_framework",
"drf_spectacular",
"djcrud_drf",
]
# urls.py
import djcrud_drf
urlpatterns = (
djcrud.site.build().urlpatterns
+ djcrud_drf.site.build().urlpatterns
)
Register ViewSets in each app’s djcrud.py (loaded by
djcrud.Site.build()). Call djcrud_drf.DrfSite.build() after
djcrud.Site.build() in urls.py. Split into other modules via imports
if you prefer — HTML ModelRouter registration in the same file
is optional and independent.
# myapp/djcrud.py
import djcrud
import djcrud_drf
from .models import Item
class ItemRouter(djcrud.ModelRouter):
model = Item
djcrud.site.routes.append(ItemRouter)
class ItemViewSet(djcrud_drf.ModelViewSet):
model = Item
djcrud_drf.site.register(ItemViewSet)
Audit logging¶
ModelViewSet mixes in LogMixin, which
writes Django admin LogEntry rows on create, update, and destroy (same
envelope format as LogMixin on HTML views). When
djcrud_history is installed, API mutations appear on each model’s history
view automatically.
Set log_actions = False on a ViewSet subclass to disable logging, or pass a
frozenset of action names ("update" covers partial_update).
Permissions¶
add_perm() and add_queryset() registered in each
app’s djcrud.py apply to HTML views and DRF ViewSets for that app. See
djcrud.permissions.
OpenAPI¶
When drf-spectacular is installed:
GET /api/schema/— OpenAPI 3 schemaGET /api/docs/— Swagger UI
The schema includes POST /api/login/ (Bearer token exchange) and CRUD routes
for every registered ModelViewSet.
Bearer security scheme¶
Use spectacular_settings() in settings.py so generated
clients know how to authenticate:
import djcrud_drf
SPECTACULAR_SETTINGS = djcrud_drf.spectacular_settings(
TITLE="My API",
)
Bearer tokens from djcrud_api work on DRF routes. See DRF API for the install walkthrough and SPA shell for the SPA shell and client codegen.