r/FastAPI • u/Adventurous_Sir1058 • Aug 29 '24
Question Getting extra param *Args and **kwargs
2
Upvotes
2
u/Adhesiveduck Aug 29 '24
Move the db_dependency into the app instance and use app.dependency_overrides:
import uvicorn
from fastapi import FastAPI, Depends
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Session
SQL_ALCHEMY_URL= "postgresql://postgres:root@localhost/users"
def get_session(session_maker: sessionmaker):
def f():
session = session_maker()
try:
yield session
finally:
session.close()
return f
def create_app():
app = FastAPI()
engine = create_engine(SQL_ALCHEMY_URL)
session_maker = sessionmaker(autocommit=False, autoflush=False, bind=engine)
app.dependency_overrides[Session] = get_session(session_maker)
return app
app = create_app()You can move the global SQL_ALCHEMY_URL, get_session wherever you want to keep it clean.


1
u/Dmitry-ADN Aug 29 '24
need code db.database.db_dependency