Advertisement
Guest User

Untitled

a guest
May 21st, 2019
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.54 KB | None | 0 0
  1. class CustomBase(object):
  2.     # Generate __tablename__ automatically
  3.     @declared_attr
  4.     def __tablename__(cls):
  5.         return cls.__name__.lower()
  6.  
  7.     # Every class has an id field, by default Integer
  8.     @declared_attr
  9.     def id(cls):
  10.         for base in cls.__mro__[1:-1]:
  11.             if getattr(base, '__table__', None) is not None:
  12.                 type = ForeignKey(base.id)
  13.                 break
  14.         else:
  15.             type = Integer
  16.  
  17.         return Column(type, primary_key=True)
  18.  
  19. Base = declarative_base(cls=CustomBase)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement