SQLAdmin

0.24.0 · active · verified Mon Apr 13

SQLAdmin is a flexible and actively developed admin interface for SQLAlchemy models, designed for use with FastAPI and Starlette. It provides a UI for managing database models, leveraging WTForms for form building and Tabler for the UI. The library maintains a regular release cadence, with updates typically occurring every few weeks to a month.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate SQLAdmin into a FastAPI application, defining a simple SQLAlchemy model and exposing it through the admin panel. It sets up an in-memory SQLite database and registers a `UserAdmin` view.

import os
from fastapi import FastAPI
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import declarative_base, sessionmaker

from sqladmin import Admin, ModelView

# 1. Database Setup
DATABASE_URL = os.environ.get("DATABASE_URL", "sqlite:///./example.db")
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})

Base = declarative_base()

class User(Base):
    __tablename__ = "users"
    id = Column(Integer, primary_key=True)
    name = Column(String, default="Anonymous")
    email = Column(String, unique=True, nullable=False)

Base.metadata.create_all(engine)

# 2. FastAPI App Setup
app = FastAPI(title="My SQLAdmin App")

# 3. SQLAdmin Setup
admin = Admin(app, engine, title="My Admin Panel")

# 4. Define ModelView
class UserAdmin(ModelView, model=User):
    column_list = [User.id, User.name, User.email]
    column_searchable_list = [User.name, User.email]
    column_sortable_list = [User.id, User.name, User.email]
    column_default_sort = ('id', True)

# 5. Add ModelView to Admin
admin.add_view(UserAdmin)

@app.get("/")
async def read_root():
    return {"message": "Welcome! Visit /admin for the admin panel."}

# To run this:
# pip install fastapi uvicorn sqladmin sqlalchemy
# uvicorn your_module_name:app --reload
# Then navigate to http://127.0.0.1:8000/admin in your browser.

view raw JSON →