2016-03-01 24 views
0

如果我有在SQLAlchemy中,有沒有辦法將父屬性映射到子對象以便快速訪問?

Category() 
    products = db.relationship('Product', backref='category') 
    name = Column(String()) 

Product() 
    name = Column(String()) 
    category_id = Column(Integer(), ForiegnKey('category.id') 

當我做

Product.query.join(Category).options(contains_eager(Product.category)).all() 

我想與產品有(在ORM說法或瞬態)一個「僞」列表CATEGORY_NAME場人口稠密落得,所以我不必詢問這樣

product.category.name 

範疇,能做到這一點,而不是

product.category_name 

感謝

回答

1

你可以用association proxies做到這一點:

class Product(Base): 
    ... 
    category_name = association_proxy("category", "name") 
+0

賓果!謝謝 :-) –

相關問題