Bottle Framework SqlAlchemy类型错误:对象不可迭代

时间:2017-02-15 05:05:24

标签: python sqlalchemy bottle

我正在尝试使用瓶子框架编写REST API。我的基类CRUD有两种方法。 基本查询执行查询,get_values方法遍历结果并返回数据。但我得到的对象不是可迭代的错误。

class InfluencerCrud(CrudObject):
def base_query(self, db_session, request_params):

    return db_session.query(Groups, GroupLeaders, Users) \
            .filter(GroupLeaders.id == Groups.group_leaders_id) \
            .filter(Users.id == GroupLeaders.users_id )

def get_values(self, db_session, request_params, raw_result):
    logging.error(raw_result)
    for group, group_leader, user in raw_result:
        data = as_dict(group, include_columns = ['clicks_estimated', 'completeness', 'cpc', 'created_at',
                                                 'description', 'guid','languages_id', 'name', 'registration_status',
                                                 'size', 'url', 'verticals_id'])
        data['addresses_id'] = user.addresses_id
        data['user'] = as_dict(user, include_columns = ["addresses_id", "created_at", "education_id", "email",
                                                        "email_verified_at", "gender", "income_id", "marital_status_id",
                                                        "name_first", "name_last", "phone", "status", "updated_at", "worK_status_id"])
        data['user']['race_id'] = []
        c = session.query(UserHasRace.race_id).filter(UserHasRace.user_id == user.id)
        for r, in c:
            data['user']['race_id'].append(r)
        data['user']['children'] = []
        c = session.query(UserChildren).filter(UserChildren.user_id == user.id)
        for r in c:
            data['user']['children'].append(get_dict_obj(r))
        picurl = None
        up = session.query(UserPicture).filter(UserPicture.id == user.pictures_id).first()
        if up != None and up.file_name != None:
            i = up.file_name.rfind('/')
            picurl = linqia.system.config.avatars.s3_url + 'user_pictures/' + up.file_name[i + 1:]
        data['user']['picture_url'] = picurl
        return data

以下是错误

  

文件“/home/linqia-api-internal/current/api-internal/source/influencers/influencer.py”,第42行,在get_values中   [Tue Feb 14 20:41:10 2017] [error] [client :: 1] for group,group_leader,raw_result中的用户:   [2月14日星期二20:41:10] [错误] [client :: 1] TypeError:'Groups'对象不可迭代

当我尝试打印原始数据时,我将获得对所有三个对象的引用。我只是没有迭代它们。

任何帮助将不胜感激,谢谢

1 个答案:

答案 0 :(得分:0)

得到了解决方法。 我的get_values函数正在调用我正在进行的每个查询,因此,它是我所做的所有查询的元组(组,组头,用户) 所以我不得不把它作为一个元组来访问。