Created
April 10, 2014 23:04
-
-
Save goFrendiAsgard/10430713 to your computer and use it in GitHub Desktop.
Extend SQL Alchemy's Base to avoid multiple inherritance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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