Yan
Yan

Reputation: 1444

how to iterate over ponyorm entity object

I am using ponyORM and i make query to the PeopleModel" for example:

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

sqlObject object return the list of PeopleModel as expected and its perfect. Now i want to print all PeopleModel values, i expect to something like that to work:

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

But it doesn't working..

How could i print all the people_model values ? Thank you vert much,

Upvotes: 3

Views: 1934

Answers (1)

Alexander Kozlovsky
Alexander Kozlovsky

Reputation: 4849

The result of select function is a query object:

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

It is not a dictionary, so it does not have items method. If you iterate over it, you will get objects. You can access objects attributes in a usual way:

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

If you want to convert object to a dictionary, you can use to_dict method:

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

Upvotes: 3

Related Questions