mirror of
https://github.com/fastapi/fastapi.git
synced 2026-04-05 23:55:06 -04:00
📝 Update docs, add first tutorials
This commit is contained in:
52
docs/tutorial/src/all/tutorial055.py
Normal file
52
docs/tutorial/src/all/tutorial055.py
Normal file
@@ -0,0 +1,52 @@
|
||||
from fastapi import FastAPI
|
||||
|
||||
from sqlalchemy import Boolean, Column, Integer, String, create_engine
|
||||
from sqlalchemy.ext.declarative import declarative_base, declared_attr
|
||||
from sqlalchemy.orm import scoped_session, sessionmaker
|
||||
|
||||
# SQLAlchemy specific code, as with any other app
|
||||
|
||||
|
||||
SQLALCHEMY_DATABASE_URI = "postgresql://user:password@postgresserver/db"
|
||||
|
||||
# By creating this a CustomBase class and inheriting from it, your models will have
|
||||
# automatic __tablename__ attributes. So you don't have to declare them.
|
||||
# So, your models will behave very similarly to, for example, Flask-SQLAlchemy
|
||||
|
||||
|
||||
class CustomBase:
|
||||
# Generate __tablename__ automatically
|
||||
@declared_attr
|
||||
def __tablename__(cls):
|
||||
return cls.__name__.lower()
|
||||
|
||||
|
||||
Base = declarative_base(cls=CustomBase)
|
||||
|
||||
|
||||
class User(Base):
|
||||
# Own properties
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
email = Column(String, unique=True, index=True)
|
||||
hashed_password = Column(String)
|
||||
is_active = Column(Boolean(), default=True)
|
||||
|
||||
|
||||
engine = create_engine(SQLALCHEMY_DATABASE_URI, convert_unicode=True)
|
||||
db_session = scoped_session(
|
||||
sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
)
|
||||
|
||||
|
||||
def get_user(username, db_session):
|
||||
return db_session.query(User).filter(User.id == username).first()
|
||||
|
||||
|
||||
# FastAPI specific code
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/users/{username}")
|
||||
def read_user(username: str):
|
||||
user = get_user(username, db_session)
|
||||
return user
|
||||
Reference in New Issue
Block a user