mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2026-03-13 07:49:55 +08:00
* fix: Change on the inheritance model, according to the documentation for the request body parameters. #171 * fix: Changes on the documentation examples to fix the inheritance problem when passing the User class (for create or update only passing the pydantic one) #171 * fix: Changes on the documentation examples to fix the inheritance problem when passing the User class (for create or update only passing the pydantic one) #171 * Put back inheritance on update model and factorize create_update_dict methods Co-authored-by: François Voron <fvoron@gmail.com>
56 lines
1.0 KiB
Python
56 lines
1.0 KiB
Python
import databases
|
|
import sqlalchemy
|
|
from fastapi import FastAPI
|
|
from fastapi_users import models
|
|
from fastapi_users.db import SQLAlchemyBaseUserTable, SQLAlchemyUserDatabase
|
|
from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base
|
|
|
|
|
|
class User(models.BaseUser):
|
|
pass
|
|
|
|
|
|
class UserCreate(models.BaseUserCreate):
|
|
pass
|
|
|
|
|
|
class UserUpdate(User, models.BaseUserUpdate):
|
|
pass
|
|
|
|
|
|
class UserDB(User, models.BaseUserDB):
|
|
pass
|
|
|
|
|
|
DATABASE_URL = "sqlite:///./test.db"
|
|
|
|
database = databases.Database(DATABASE_URL)
|
|
|
|
Base: DeclarativeMeta = declarative_base()
|
|
|
|
|
|
class UserTable(Base, SQLAlchemyBaseUserTable):
|
|
pass
|
|
|
|
|
|
engine = sqlalchemy.create_engine(
|
|
DATABASE_URL, connect_args={"check_same_thread": False}
|
|
)
|
|
|
|
Base.metadata.create_all(engine)
|
|
|
|
users = UserTable.__table__
|
|
user_db = SQLAlchemyUserDatabase(UserDB, database, users)
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
@app.on_event("startup")
|
|
async def startup():
|
|
await database.connect()
|
|
|
|
|
|
@app.on_event("shutdown")
|
|
async def shutdown():
|
|
await database.disconnect()
|