Sqlalchemy session.query结果没有列名

时间:2016-02-01 03:33:27

标签: sql sqlalchemy flask-sqlalchemy

        all_hero = db.session.query(HeroModel.hero_metadata_id, HeroModel.enchant_level, HeroModel.is_evolve,
                                HeroModel.skill_1_level, HeroModel.skill_2_level, HeroModel.skill_3_level,
                                HeroModel.equipment_id_accessory, HeroModel.equipment_id_weapon,
                                HeroModel.soul_stone, HeroModel.state).\
        filter(HeroModel.id.in_(hero_ids)). \
        all()

我无法使用' all_hero.state'

等属性访问all_hero

并且结果也是这样打印的     {     "英雄":[         [

       0: 1,
       1: 0,
       2: false,

...

我希望结果是

' hero_metadata_id':1,

' hero_enchant_level:0

...

帮帮我......

1 个答案:

答案 0 :(得分:0)

您提供了要查询的列的列表,因此SQLAlchemy通过返回列列表进行响应。如果您希望对象访问您的属性,则应该只查询HeroModel实体。

# returns a list of model instances matching the ids in hero_ids
all_hero = db.session.query(HeroModel).\
        filter(HeroModel.id.in_(hero_ids)). \
        all()

# access attributes on the model instances
for hero_instance in all_hero:
    print(hero_instance.hero_metadata_id)