2014-12-09 2 views
0

없이 여러 유형의 배열 I 그래서SQLAlchemy의 - 상속

class User(Base): 
    __tablename__ = 'users' 

    id = Column(Integer, primary_key=True) 
    username = Column(String) 
    first_name = Column(String) 
    last_name = Column(String) 
    email = Column(String) 


user_group = Table('user_group', Base.metadata, 
        Column('user_id', Integer, ForeignKey('users.id')), 
        Column('group_id', Integer, ForeignKey('groups.id')) 
        ) 


class Group(Base): 
    __tablename__ = 'groups' 

    id = Column(Integer, primary_key=True) 
    name = Column(String) 
    members = relationship("User", secondary=user_group) 

같은 SQLAlchemy의 모델의 기존 설정을하고 난 중 하나 User 또는 많은 관계로 일을 할 수 있습니다 세 번째 모델을 만들고 싶습니다 a Group 유형. 그래서처럼 사용할 수있는 무언가와 끝까지 할이

class Contact(Base): 
    __tablename__ = 'contacts' 

    id = Column(Integer, primary_key=True) 
    type_id = Column(Integer) # the id of the group or user row which it refers to. 
    type = Column(String) # 'group' or 'user' 

class Thing(Base): 
    __tablename__ = 'things' 
    relationship('Contact') # Array of Contacts 
thing = Thing() 
thing.contacts[0].type # returns 'group' or 'user' 
thing.contacts[0].contact # returns the actual User or Group object 

내가이가 상속 할 수 있습니다 알고 있지만 어떤 대안이

? 차라리 기본 사용자 및 그룹 모델을 서브 클래 싱하지 않아도됩니다.

미리 감사드립니다.

답변