Admin Panel¶
P8s includes a Django-style admin panel for managing your data models.
Overview¶
The admin panel provides:
- CRUD operations for all registered models
- Search & filtering
- Pagination
- Bulk actions (delete, export)
- User authentication
Accessing the Admin¶
Navigate to:
Authentication¶
The admin panel requires authentication:
- Not logged in → Shows login page
- Logged in as superuser → Full access to dashboard
- Logged in as regular user → "Invalid credentials" error
Create a superuser to access admin:
Registering Models¶
Models appear in the admin automatically if they use @register_model:
from p8s import Model
from p8s.admin import register_model
from sqlmodel import Field
@register_model
class Product(Model, table=True):
name: str = Field(index=True)
price: float
is_active: bool = True
class Admin:
list_display = ["name", "price", "is_active"]
search_fields = ["name"]
list_filter = ["is_active"]
Admin Configuration¶
Customize admin behavior with the inner Admin class:
list_display¶
Fields shown in the list view:
search_fields¶
Fields searchable via the search box:
list_filter¶
Fields available for filtering:
ordering¶
Default sort order:
readonly_fields¶
Fields that cannot be edited:
Complete Example¶
@register_model
class Article(Model, table=True):
title: str
content: str
published: bool = False
author_id: UUID | None = None
class Admin:
list_display = ["title", "published", "created_at"]
search_fields = ["title", "content"]
list_filter = ["published"]
ordering = ["-created_at"]
readonly_fields = ["created_at", "updated_at"]
Admin API Endpoints¶
The admin exposes REST endpoints under /admin/:
| Method | Endpoint | Description |
|---|---|---|
| GET | /admin/ |
Admin UI (HTML) |
| GET | /admin/models |
List registered models |
| GET | /admin/models/{name} |
Model metadata |
| GET | /admin/{model} |
List records |
| POST | /admin/{model} |
Create record |
| GET | /admin/{model}/{id} |
Get record |
| PATCH | /admin/{model}/{id} |
Update record |
| DELETE | /admin/{model}/{id} |
Delete record |
| POST | /admin/{model}/bulk-delete |
Bulk delete |
All API endpoints require admin authentication.
Security¶
Protected by default¶
- All admin endpoints require
require_admindependency - Uses JWT Bearer token authentication
- OpenAPI docs (
/docs) are also admin-protected
CORS Configuration¶
Ensure your CORS settings allow the admin UI:
Customizing the Admin¶
Admin Settings¶
Configure admin behavior in settings.py:
class AppSettings(Settings):
admin: AdminSettings = AdminSettings(
enabled=True,
path="/admin",
title="My App Admin",
)