如何遍历 ponyorm 实体对象

how to iterate over ponyorm entity object

我正在使用 ponyORM 并查询 PeopleModel" 例如:

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

sqlObject object return PeopleModel 列表符合预期,非常完美。 现在我想打印所有 PeopleModel 值,我希望类似的东西能工作:

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

但是没用..

如何打印所有 people_model 值? 非常感谢,

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)