Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sqlalchemy import create_engine, Column, Integer
- from sqlalchemy.ext.declarative import declarative_base, declared_attr
- from sqlalchemy.orm import sessionmaker
- import settings
- engine = create_engine(settings.connection_uri)
- BaseSession = sessionmaker(bind=engine)
- class Singleton(type):
- def __init__(cls, *args, **kwargs):
- super(Singleton, cls).__init__(*args, **kwargs)
- cls.instance = None
- def __call__(cls, *args, **kwargs):
- if cls.instance is None:
- cls.instance = super(Singleton, cls).__call__(*args, **kwargs)
- return cls.instance
- class Session(BaseSession):
- __metaclass__ = Singleton
- class Base(object):
- @declared_attr
- def __tablename__(cls):
- return cls.__name__
- session = Session()
- id = Column(Integer, primary_key=True)
- def save(self):
- self.session.add(self)
- self.session.commit()
- Base = declarative_base(cls=Base)
- class TestTable(Base):
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement