Quick start¶
This page takes you from an installed package to a working admin dashboard.
It assumes you finished Installation and exported the
required ADMIN_* environment variables.
1. Mount the admin app¶
2. Register a model admin¶
Register the ORM model named by ADMIN_USER_MODEL first — it is required for
sign-in. Pick the admin base class matching your ORM
(TortoiseModelAdmin, DjangoModelAdmin, SqlAlchemyModelAdmin or
PonyORMModelAdmin):
import bcrypt
from fastadmin import TortoiseModelAdmin, register
from models import User
@register(User)
class UserAdmin(TortoiseModelAdmin):
exclude = ("hash_password",)
list_display = ("id", "username", "is_superuser", "is_active")
list_display_links = ("id", "username")
list_filter = ("id", "username", "is_superuser", "is_active")
search_fields = ("username",)
async def authenticate(self, username: str, password: str) -> int | None:
user = await User.filter(username=username, is_superuser=True).first()
if not user:
return None
if not bcrypt.checkpw(password.encode(), user.hash_password.encode()):
return None
return user.id
async def change_password(self, id: int, password: str) -> None:
user = await User.filter(id=id).first()
if not user:
return
user.hash_password = bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
await user.save(update_fields=("hash_password",))
Warning
The model admin for ADMIN_USER_MODEL must implement authenticate
(and change_password if you want password editing). See
Authentication.
Note
Adapt the field names (hash_password, is_active, …) to your own user
model — the snippet above shows the recommended bcrypt-hashed setup.
3. Run it¶
Start your app as usual (e.g. uvicorn example:app for FastAPI) and open
http://localhost:8000/admin. Sign in with a user for which authenticate
returns an id.
Next steps¶
- Registering models — complete, runnable examples for all four ORMs.
- Model admins — all attributes and hooks
(
list_display, actions, permissions, exports, …). - Dashboard widgets — add charts to the dashboard.