与sqlalchemy嵌套选择

时间:2018-08-22 10:00:30

标签: python datatable sqlalchemy flask-sqlalchemy

是否可以在单个查询中获得所有产品及其类别作为(id_category,名称)的列表/字典?我一直在寻找类似的东西,但找不到如何正确命名此问题,所以我也找不到解决方案。我正在提供某种数据表,并希望在一个页面中显示产品类别,或者在另一子页面中更好地处理它?以这种方式处理它是一种好习惯吗?

示例表格模型:

class Category(Model):
    id_category = Column(Integer, Sequence('seq_id_category'), primary_key=True, nullable=False)                                                         
    name = Column(UnicodeText, nullable=False, unique=True)                                  


class Product(Model):                                 
    id_product = Column(Integer, Sequence('seq_id_product'), primary_key=True, nullable=False)                                
    name = Column(Text, nullable=False)
    categories = relationship('ProductCategory') 


class ProductCategory(Model):                                                          
    __table_args__ = (                                                                         
    PrimaryKeyConstraint('id_product', 'id_category', name='seq_id_product_category')
    )                                                                                          

    id_product = Column(Integer, ForeignKey(Product.id_product), nullable=False)
    id_category = Column(Integer, ForeignKey(Category.id_category), nullable=False)

0 个答案:

没有答案
相关问题