3

我正在使用 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 值?非常感谢,

4

1 回答 1

3

函数的结果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)
于 2017-03-01T12:23:01.620 回答