@@ -25,7 +25,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
2525
2626# Minimal CRUDAdmin instance
2727admin = CRUDAdmin(
28- session = your_async_session , # Your SQLAlchemy async session
28+ session = get_session , # Your session dependency function
2929 SECRET_KEY = " your-secret-key-here"
3030)
3131```
@@ -43,12 +43,16 @@ from sqlalchemy.orm import sessionmaker
4343# Database setup
4444DATABASE_URL = " sqlite+aiosqlite:///./app.db"
4545engine = create_async_engine(DATABASE_URL , echo = True )
46- async_session = sessionmaker(engine, class_ = AsyncSession, expire_on_commit = False )
46+
47+ # Create database session dependency
48+ async def get_session ():
49+ async with AsyncSession(engine) as session:
50+ yield session
4751
4852# Create admin with common settings
4953admin = CRUDAdmin(
5054 # Required parameters (no defaults)
51- session = async_session ,
55+ session = get_session ,
5256 SECRET_KEY = os.environ.get(" ADMIN_SECRET_KEY" , " dev-key-change-in-production" ),
5357
5458 # Basic interface settings
@@ -70,7 +74,7 @@ admin = CRUDAdmin(
7074
7175- ** Required parameters** : ` session ` and ` SECRET_KEY ` have no defaults and must be provided
7276- ** Optional parameters** : All other parameters have sensible defaults and can be omitted
73- - ** Most minimal setup** : ` CRUDAdmin(session=async_session , SECRET_KEY="your-key") ` uses all defaults
77+ - ** Most minimal setup** : ` CRUDAdmin(session=get_session , SECRET_KEY="your-key") ` uses all defaults
7478
7579---
7680
@@ -82,10 +86,12 @@ admin = CRUDAdmin(
8286Your SQLAlchemy async session factory or callable that returns sessions:
8387
8488``` python
85- # Session factory (most common)
86- from sqlalchemy.orm import sessionmaker
87- async_session = sessionmaker(engine, class_ = AsyncSession, expire_on_commit = False )
88- admin = CRUDAdmin(session = async_session, SECRET_KEY = secret_key)
89+ # Session dependency function (recommended)
90+ async def get_session ():
91+ async with AsyncSession(engine) as session:
92+ yield session
93+
94+ admin = CRUDAdmin(session = get_session, SECRET_KEY = secret_key)
8995```
9096
9197#### ` SECRET_KEY ` (str)
@@ -94,7 +100,7 @@ Critical for session security and cookie signing. **Never use default values in
94100``` python
95101# ✅ Use environment variables
96102admin = CRUDAdmin(
97- session = session ,
103+ session = get_session ,
98104 SECRET_KEY = os.environ[" ADMIN_SECRET_KEY" ]
99105)
100106
@@ -109,11 +115,11 @@ URL path where the admin interface will be accessible. **If you want a different
109115
110116``` python
111117# Default: accessible at /admin (no mount_path parameter needed)
112- admin = CRUDAdmin(session = session , SECRET_KEY = key)
118+ admin = CRUDAdmin(session = get_session , SECRET_KEY = key)
113119
114120# Custom path: accessible at /dashboard (must specify mount_path)
115121admin = CRUDAdmin(
116- session = session ,
122+ session = get_session ,
117123 SECRET_KEY = key,
118124 mount_path = " /dashboard" # Required for non-default paths
119125)
@@ -134,22 +140,22 @@ Choose between light and dark themes:
134140
135141``` python
136142# Dark theme (default)
137- admin = CRUDAdmin(session = session , SECRET_KEY = key, theme = " dark-theme" )
143+ admin = CRUDAdmin(session = get_session , SECRET_KEY = key, theme = " dark-theme" )
138144
139145# Light theme
140- admin = CRUDAdmin(session = session , SECRET_KEY = key, theme = " light-theme" )
146+ admin = CRUDAdmin(session = get_session , SECRET_KEY = key, theme = " light-theme" )
141147```
142148
143149#### ` admin_db_path ` (str, default: None)
144150Custom location for the admin database (used for admin users, sessions, etc.):
145151
146152``` python
147153# Default: creates ./crudadmin_data/admin.db
148- admin = CRUDAdmin(session = session , SECRET_KEY = key)
154+ admin = CRUDAdmin(session = get_session , SECRET_KEY = key)
149155
150156# Custom path
151157admin = CRUDAdmin(
152- session = session ,
158+ session = get_session ,
153159 SECRET_KEY = key,
154160 admin_db_path = " ./admin/admin_database.db"
155161)
@@ -160,11 +166,11 @@ Automatically create an admin user when the system initializes:
160166
161167``` python
162168# No initial admin (default - create manually later)
163- admin = CRUDAdmin(session = session , SECRET_KEY = key)
169+ admin = CRUDAdmin(session = get_session , SECRET_KEY = key)
164170
165171# Create initial admin automatically
166172admin = CRUDAdmin(
167- session = session ,
173+ session = get_session ,
168174 SECRET_KEY = key,
169175 initial_admin = {
170176 " username" : " admin" ,
@@ -202,7 +208,7 @@ app.mount("/admin", admin.app)
202208``` python
203209# If you configured a custom mount_path
204210admin = CRUDAdmin(
205- session = async_session ,
211+ session = get_session ,
206212 SECRET_KEY = key,
207213 mount_path = " /dashboard"
208214)
@@ -220,7 +226,7 @@ app.mount("/dashboard", admin.app)
220226``` python
221227# Simple development configuration
222228admin = CRUDAdmin(
223- session = async_session ,
229+ session = get_session ,
224230 SECRET_KEY = " dev-key-change-in-production" , # Simple key for development
225231 initial_admin = { # Convenient auto-admin
226232 " username" : " admin" ,
@@ -234,7 +240,7 @@ admin = CRUDAdmin(
234240``` python
235241# Basic production configuration
236242admin = CRUDAdmin(
237- session = async_session ,
243+ session = get_session ,
238244 SECRET_KEY = os.environ[" ADMIN_SECRET_KEY" ], # Required environment variable
239245 initial_admin = None , # Create admin users manually
240246 secure_cookies = True , # Default: True (good for production)
0 commit comments