Skip to content

Instantly share code, notes, and snippets.

@goFrendiAsgard
Created April 10, 2014 23:04
Show Gist options
  • Save goFrendiAsgard/10430713 to your computer and use it in GitHub Desktop.
Save goFrendiAsgard/10430713 to your computer and use it in GitHub Desktop.
Extend SQL Alchemy's Base to avoid multiple inherritance
from sqlalchemy.ext.declarative import declarative_base, declared_attr
from sqlalchemy import Column, Integer, String
Base = declarative_base()
class CustomBase(Base):
__abstract__ = True
def __init__(self):
print "Hello world"
@declared_attr
def __tablename__(cls):
return cls.__name__.lower()
class Person(CustomBase):
__tablename__ = 'orang'
id = Column(Integer, primary_key=True)
nama = Column(String)
if __name__ == '__main__':
person = Person()
person.nama = 'Tono'
print person.__tablename__
print person.nama
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment