Make a connection
from sqlalchemy import create_engine
def get_engine():
return create_engine("sqlite:///mode_examples.sqlite")
Make a session
from sqlalchemy.orm import sessionmaker
def get_session():
con = get_engine()
Base.bind = con
Base.metadata.create_all()
Session = sessionmaker(bind=con)
session = Session()
return session
Make a Base Class
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
Base.metadata.bind = get_engine()
Make your First Model
class User(Base):
__tablename__ = "users"
username = Column('username', Text())
firstname = Column('firstname', Text())
lastname = Column('lastname', Text())
Make your own Base Class to inherit From
class MyBaseHelper:
def to_dict(self):
return {k: v for k, v in self.__dict__.items() if k[0] != "_"}
def update(self, **attrs):
for key, value in attrs.items():
if hasattr(self, key):
setattr(self, key, value)
Use the Custom Base Class
class User(Base, MyBaseHelper):
__tablename__ = "users"
username = Column('username', Text())
firstname = Column('firstname', Text())
lastname = Column('lastname', Text())