2014-03-07 2 views
1

SQLAlchemy를 사용하여 클래스 상속에 문제가 있습니다.SQLAlchemy 클래스 및 클래스를 상속하는 방법

다음 분. 예 내가하고 싶은 것을 보여줍니다 내 문제가 무엇인지 :

class Foo(object): 
    bar = 'hello world' 

class Car(Base, Foo): 
    id = Column(Integer, primary_key=True) 
    color = Column(String(50)) 

if __name__ == '__main__': 
    # 'bar' attribute is available on new class instance 
    my_car = Car() 
    print my_car.bar # "hello world" 

    # 'bar' attribute is not available on query init 
    db_session = init_db() 
    my_car = db_session.query(Car).first() 
    print my_car.bar # Error 

내가 SQLAlchemy의의 Base 클래스에서 상속 Car 클래스가 있습니다. 그 외에도 Car 클래스는 Foo 클래스에서 상속합니다. 이 예에서 Foo 클래스는 단지 bar 속성으로 구성됩니다.

Car 인스턴스가 SQLAlchemy의 쿼리 기능을 통해 인스턴스화되면 bar 특성에 액세스 할 수 없습니다. SQLAlchemy가 클래스를 인스턴스화하는 방법과 나에게 해결 방법이 있습니까?

저는 SQLAlchemy 0.7을 사용하고 있습니다.

답변

0

당신은 모든 클래스에 대한 기본 기본적 object을 대체 할 declarative_basecls 매개 변수를 사용할 수 있습니다 :

Base = declarative_base(cls=MyBaseClass) 

이 옵션에 대한 자세한 내용은, 문서의 Mixin and Custom Base Classes 섹션을 참조하십시오.

관련 문제