如何迭代ponyorm实体对象

时间:2017-02-21 15:16:00

标签: python postgresql orm ponyorm

我正在使用ponyORM,我向PeopleModel"例如:

first_name = "avi"
sqlObject = select(p for p in PeopleModel if raw_sql('( lower(first_name) = lower($first_name))

sqlObject对象按预期返回PeopleModel列表并将其完美。 现在我想要打印所有PeopleModel值,我期望这样的工作:

for people_model_key,people_model_value in sqlObject.items():
    print(people_model_value)

但它没有工作..

我怎样才能打印所有people_model值? 谢谢你,

1 个答案:

答案 0 :(得分:2)

select函数的结果是查询对象:

first_name = "avi"
people = select(p for p in PeopleModel
                if raw_sql('( lower(first_name) = lower($first_name))

它不是字典,因此它没有items方法。如果你迭代它,你会得到对象。您可以通常的方式访问对象属性:

for person in people:
    print('name:', person.name)
    print('age:', person.age)

如果要将对象转换为字典,可以使用to_dict方法:

for person in people:
    for key, value in person.to_dict().items():
        print(key, value)